当发布类型产品从待定状态发布时发送电子邮件通知

时间:2018-08-22 作者:Puspalata Panigrahi

我想向受尊敬的用户发送电子邮件,该用户的产品正在从挂起状态获得发布。我正在使用以下代码,但没有收到邮件。

add_action("publish_post", "on_publish_pending_post", 10, 1);

function on_publish_pending_post($post) {

    // A function to perform actions when a post is published.

    if ($post->post_type == \'product\') {

        $name = get_the_title($post->ID);

        // get email from custom field
        // $author = get_post_meta( $post->ID, $user_email, true );

        $to_email = $author->user_email;

        echo $to_email;
        exit();

        $fromMail = "[email protected]";
        $subjectMail = "Design Approval Success";
        $content = \'<p>Your Design has been published !!!</p>\';

        $headersMail  = \'\';
        $headersMail .= \'From: \' . $fromMail . "\\r\\n";
        $headersMail .= \'Reply-To: \' . $fromMail . "\\r\\n";
        $headersMail .= \'MIME-Version: 1.0\' . "\\r\\n";
        $headersMail .= \'Content-Type: text/html; charset=ISO-8859-1\' . "\\r\\n";

        mail($to_email, $subjectMail, $content, $headersMail);

    }

}

1 个回复
SO网友:nmr

你很接近。您试图使用的挂钩是从函数调用的wp_transition_post_status():

function wp_transition_post_status( $new_status, $old_status, $post ) {
   // 
   // ...
   //
   do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );
}
因此,要在以下情况下执行代码:product 状态更改将函数添加到挂钩publish_product.
传递给函数的第一个参数是$post_id, 不WP_Post 对象
最后一件事,检查功能中的帖子类型on_publish_pending_post 没有必要,因为挂钩用于特定类型(“发布_post“,”发布_product“”。

Try this code:

add_action("publish_product", "on_publish_pending_post", 10, 2);

function on_publish_pending_post($post_id, $post) {

    $auth_id = (int)$post->post_author;
    $user = get_userdata($auth_id);
    if ( $user === false )
        return;
    $to_email = $user->user_email;

    $name = get_the_title($post_id);

    $fromMail = "[email protected]";
    $subjectMail = "Design Approval Success";
    $content = \'<p>Your Design has been published !!!</p>\';

    $headersMail  = \'\';
    $headersMail .= \'From: \' . $fromMail . "\\r\\n";
    $headersMail .= \'Reply-To: \' . $fromMail . "\\r\\n";
    $headersMail .= \'MIME-Version: 1.0\' . "\\r\\n";
    $headersMail .= \'Content-Type: text/html; charset=ISO-8859-1\' . "\\r\\n";

    // ------ 
    // echo "<br>recipient: " . htmlspecialchars($to_email);
    // echo "<br>sender: " . htmlspecialchars($fromMail);
    // echo "<br>post_title: " . htmlspecialchars($name);
    // exit();
    // ------

    mail($to_email, $subjectMail, $content, $headersMail);
}

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register