发布状态时发送wordpress发送电子邮件

时间:2017-03-21 10:05:17

标签: php wordpress

我有一个由前端访客提交的自定义帖子。访问者提交的帖子状态正在等待中。

现在,当管理员将帖子状态从待处理更改为发布时,我想向此帖子的作者发送电子邮件。自定义字段收集的作者电子邮件。

function send_mails_on_publish( $new_status, $old_status, $post ) {
    if ( 'publish' !== $new_status or 'publish' === $old_status or 'trainee' !== get_post_type( $post ) )
        return;

    $author = get_post_meta( $post_id, $tr_user_reg_email, true );

    $body = sprintf( 'Hey there is a new entry!
        See <%s>',
        get_permalink( $post )
    );


    wp_mail( $author, 'New entry!', $body );
}
add_action( 'transition_post_status', 'send_mails_on_publish', 10, 3 );

这就是我正在尝试的。但这不起作用。有人可以帮帮我吗?在此先感谢:)

2 个答案:

答案 0 :(得分:1)

您的托管服务提供商是否有任何电子邮件限制?特别是如果它是免费托管。如果是这样,那可能是它不起作用的原因。如果没有,它可能只是一个小错字。在我看来,你也错过了if语句的{}。

答案 1 :(得分:0)

是的,我从post status transition

得到了答案
function on_publish_pending_post( $post ) {
// A function to perform actions when a post is published.

if ( "trainee" === get_post_type() ) { // check the custom post type

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

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

    $subject = "mail subject";

    $body    = "mail body";

    $headers = array (
        'From: "your name" <no-reply@your-domain.com>' ,
        'X-Mailer: PHP/' . phpversion(),
        'MIME-Version: 1.0' ,
        'Content-type: text/html; charset=iso-8859-1'
    );
    $headers = implode( "\r\n" , $headers );

    wp_mail( $author, $subject, $body, $headers );
}

} add_action(&#34; pending_to_publish&#34;,&#34; on_publish_pending_post&#34;,10,1);

相关问题