在发布状态更改时通知作者

时间:2020-11-05 12:06:20

标签: wordpress woocommerce

我正在Wordpress / Woocommerce网站上工作,“广告商”可以在该网站上发布自己的产品。

发布产品有两个步骤,因此,当用户提交产品时,状态设置为“待审核”,我添加了新的发布状态“最终验证”,请问有什么方法可以使用当我们将“发布”状态更改为“最终验证”时,可以使用诸如publish_post挂钩之类的方法向作者发送电子邮件? 我认为我可以轻松地编辑下面的代码,只说“如果状态更改=“最终验证”,但是我并没有真正到达目的地。显然,add_action不应为“ publish_post”,但我不确定应该是什么。

有人可以帮忙吗?

function notifyauthor($post_id) {
 
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "We are validating your post : ".$post->post_title."";
 
$message = "
      Hi ".$author->display_name.",
       
      Your product, \"".$post->post_title."\" is now in the final stages of being validated.
       
      View post: ".get_permalink( $post_id )."
       
      Thanks"
      ;
       
   wp_mail($author->user_email, $subject, $message);
}
add_action('publish_post', 'notifyauthor');

1 个答案:

答案 0 :(得分:0)

设法使它起作用。 如果发布状态从“待审核”更改为“已验证”,则会触发电子邮件。

在功能文件中


function authorNotification( $new_status, $old_status, $post ) {
    if ( $new_status == 'verified' && $old_status != 'pending review' ) {
        $headers = 'From: MyName <admin@MyDomain.com>' . "\r\n";
        $author = get_userdata($post->post_author);
        $message = "
            Hello ".$author->display_name.",
            We have verified your offer and are ready to publish:  ".$post->post_title." 
            
        ";
        wp_mail($author->user_email, "We have verified your offer", $message, $headers);
    }
}
add_action('transition_post_status', 'authorNotification', 10, 3 );
相关问题