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

图片转代码服务由CSDN问答提供 功能建议

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

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

  function send_mails_on_publish($ new_status,$ old_status,$ post){
 if('publish'!== $ new_status或'publish'  === $ old_status或'trainee'!== get_post_type($ post))
 return; 
 
 $ author = get_post_meta($ post_id,$ tr_user_reg_email,true); 
 
 $ body = sprintf(' 嘿有一个新条目!
请参阅&lt;%s&gt;',
 get_permalink($ post)
); 
 
 
 wp_mail($ author,'New entry!',$ body); \  n} 
add_action('transition_post_status','send_mails_on_publish',10,3); 
   
 
 

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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

    已采纳该答案
    打赏 评论
  • dsj1961061 2017-03-21 10:19

    Do you have any email restrictions from your hosting provider? Especially if it's a free hosting. If so, that may be the reason for it not working. If not, it may be just a small typo. It looks to me that you are also missing the {} for your if statement.

    打赏 评论

相关推荐 更多相似问题