【问题标题】:Wordpress send email to selected user when custom post type is created创建自定义帖子类型时,Wordpress 向选定用户发送电子邮件
【发布时间】:2015-12-03 19:28:47
【问题描述】:

我正在构建一个 wordpress 插件,它将自定义帖子类型“预订”添加到仪表板。此帖子类型允许管理员创建预订表单并将其分配给用户(从用户下拉列表中) - 此信息(即所选用户)存储在 wp_postmeta DB 表中作为“wedding_user”。

我想做的是在创建预订(帖子类型)时向所选用户发送电子邮件。

这是我目前所拥有的,我知道这将在创建自定义帖子类型时发送一封电子邮件,但我如何在其中添加选定的用户电子邮件地址?

function notify_client($post_ID)  {
// a conditional to check that this is a new post
if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {

    $user_email_address = USER EMAIL ADDRESS

    // create the from details 
    $headers[] = 'From: Bookings <bookings@thedistractionsband.co.uk>';
    // lets cc in the head just because we can 
    $headers[] = 'Cc: Joseph Malik <sam.skirrow@gmail.com>';
    // separate the users array
    $send_to = $user_email_address;
    // concatenate a message together
    $message = 'Test Message';
    // and finally send the email
    wp_mail($send_to, "Test Message", $message, $headers );
    return $post_ID;
    }
}
add_action('publish_bookings', 'notify_client');

var_dump($_POST);的输出

array(59) { 
    ["_wpnonce"]=> string(10) "0d5335d18f" 
    ["_wp_http_referer"]=> string(60) "/distractions-login/wp-admin/post-new.php?post_type=bookings" 
    ["user_ID"]=> int(1) 
    ["action"]=> string(8) "editpost" 
    ["originalaction"]=> string(8) "editpost" 
    ["post_author"]=> int(1) 
    ["post_type"]=> string(8) "bookings" 
    ["original_post_status"]=> string(10) "auto-draft" 
    ["referredby"]=> string(79) "http://www.skizzar.com/distractions-login/wp-admin/post.php?post=80&action=edit" 
    ["_wp_original_http_referer"]=> string(79) "http://www.skizzar.com/distractions-login/wp-admin/post.php?post=80&action=edit" 
    ["auto_draft"]=> string(1) "1" 
    ["post_ID"]=> string(2) "81" 
    ["meta-box-order-nonce"]=> string(10) "14fd434c70" 
    ["closedpostboxesnonce"]=> string(10) "2b04c74cd9" 
    ["post_title"]=> string(15) "Test Booking 13" 
    ["samplepermalinknonce"]=> string(10) "4589b08166" 
    ["wp-preview"]=> string(0) "" 
    ["hidden_post_status"]=> string(5) "draft" 
    ["post_status"]=> string(7) "publish" 
    ["hidden_post_password"]=> string(0) "" 
    ["hidden_post_visibility"]=> string(6) "public" 
    ["visibility"]=> string(6) "public" 
    ["post_password"]=> string(0) "" 
    ["mm"]=> string(2) "12" 
    ["jj"]=> string(2) "03" 
    ["aa"]=> string(4) "2015" 
    ["hh"]=> string(2) "11" 
    ["mn"]=> string(2) "05" 
    ["ss"]=> string(2) "06" 
    ["hidden_mm"]=> string(2) "12" 
    ["cur_mm"]=> string(2) "12" 
    ["hidden_jj"]=> string(2) "03" 
    ["cur_jj"]=> string(2) "03" 
    ["hidden_aa"]=> string(4) "2015" 
    ["cur_aa"]=> string(4) "2015" 
    ["hidden_hh"]=> string(2) "11" 
    ["cur_hh"]=> string(2) "11" 
    ["hidden_mn"]=> string(2) "05" 
    ["cur_mn"]=> string(2) "05" 
    ["original_publish"]=> string(7) "Publish" 
    ["publish"]=> string(7) "Publish" 
    ["booking_details"]=> string(10) "54291c7a09" 
    ["wedding_name"]=> string(15) "Test Booking 13" 
    ["wedding_user"]=> string(9) "Test User" 
    ["wedding_date"]=> string(10) "30/12/2015" 
    ["wedding_package"]=> string(9) "Package 1" 
    ["wedding_price"]=> string(5) "45768" 
    ["wedding_payment_due_date"]=> string(10) "28/01/2016" 
    ["wedding_hidden_todays_date"]=> string(10) "03/12/2015" 
    ["wedding_hidden_is_viewed"]=> string(0) "" 
    ["wedding_hidden_is_completed"]=> string(0) "" 
    ["wedding_hidden_date_completed"]=> string(0) "" 
    ["deposit_paid"]=> string(0) "" 
    ["full_paid"]=> string(0) "" 
    ["post_name"]=> string(0) "" 
    ["post_mime_type"]=> string(0) "" 
    ["ID"]=> int(81) 
    ["comment_status"]=> string(6) "closed" 
    ["ping_status"]=> string(6) "closed" 
} 

以下是用于在自定义帖子类型中呈现元框的代码:

public function bookings_meta_boxes() {
    add_meta_box(
        'booking_details',
        'Booking Details',
        array( $this, 'render_meta_boxes' ),
        'bookings',
        'normal',
        'high'
    );
}

function render_meta_boxes( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'booking_details' ); 
    $prfx_stored_meta = get_post_meta( $post->ID );

<p>
<label for="wedding_user" class="prfx-row-title"><?php _e( 'Wedding Guest', 'prfx-textdomain' )?></label>
<select name="wedding_user" id="wedding_user">
    <?php
    $users = get_users();
    $i = 0;
    // Array of WP_User objects.
    foreach ( $users as $user ) {
        echo "<option value='".esc_html( $user->display_name )."' ";
        if (isset($prfx_stored_meta['wedding_user']))
        echo selected($prfx_stored_meta['wedding_user'][0], esc_html( $user->display_name ));
        echo ">" . esc_html( $user->display_name ) . "</option>";
    }
    ?>

</select>
</p>

...

}

【问题讨论】:

  • get_userdata($_POST['wedding_user'])-&gt;user_email; 或类似的 - 如果没有更多信息,就没有真正的帮助
  • 感谢@Steve - 不幸的是它没有用。我可以给你什么额外的信息?
  • 婚礼用户在帖子中是如何存储的?作为元字段?基本上,您会在 $_POST 数据中的某处找到用户 ID(我以 $_POST['wedding_user'] 为例,因为我不知道确切的结构) - 如果您使用像 xdebug 这样的调试器,请设置断点并检查 $_POST - 如果不是var_dump($_POST);,那么您将能够看到如何访问用户 ID
  • @Steve 是的,wedding_user 被存储为一个元字段(所以在数据库中的 wp_postmeta 表中)——这会让事情更清楚吗?
  • 你在你的函数中检查了$_POST的内容吗?

标签: php wordpress email


【解决方案1】:

更改您的元框代码以使选项值属性包含用户电子邮件而不是 display_name:

foreach ( $users as $user ) {
    echo "<option value='".$user->user_email."' "; //<--here
    if (isset($prfx_stored_meta['wedding_user']))
        echo selected($prfx_stored_meta['wedding_user'][0], $user->user_email ); //<-- and here
    echo ">" . esc_html( $user->display_name ) . "</option>";
}

然后在notify_client函数中:

$user_email_address = $_POST['wedding_user'];

【讨论】:

  • 太好了,很高兴我能帮上忙
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 2018-02-08
  • 1970-01-01
  • 2015-11-06
相关资源
最近更新 更多