dongqie8661 2017-03-21 10:05
浏览 30
已采纳

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

I have a custom post which is submitted by the visitor from front end. Visitor's submitted post status is pending.

Now when admin change the post status from pending to publish, I want to send an email to the author of this post. author email collected by custom field.

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 );

This is what I'm trying. But this does not work. Anybody can help me? Thanks in advance :)

  • 写回答

2条回答 默认 最新

  • doumang20060820 2017-03-22 09:04
    关注

    Yeh got my answer from 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( "
    " , $headers );
    
        wp_mail( $author, $subject, $body, $headers );
    }
    

    } add_action( "pending_to_publish", "on_publish_pending_post", 10, 1 );

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?