仅在发布自定义帖子类型帖子时向作者发送电子邮件,而不是在以后编辑时发送电子邮件

时间:2014-04-21 作者:Chris

我有以下功能,可以向custom post type 当这篇文章发表时,这很好,但我希望它不会在以后的任何编辑中给他们发电子邮件,以避免意外地给他们发送大量电子邮件。

这是我目前的职能:

//email authors
function authorNotification($post_id) {
   $post = get_post($post_id);
   $author = get_userdata($post->post_author);

   $message = "Hi ".$author->display_name.".
A document for you, ".$post->post_title." has just been published or edited and requires your attention. 
You will be taken to the document after you log in from this link: ".get_permalink( $post_id ).".
   ";
   wp_mail($author->user_email, "An online document has been made or edited for you.", $message);
}
add_action(\'publish_portaldocuments\', \'authorNotification\');
我想知道它是否和{$old\\u status}to{$new\\u status}和{$new\\u status}{$post->post\\u type}挂钩有关,如本页所示:Post publish only hook?

有人能帮忙吗,因为我不知道如何用自定义的帖子类型实现它。谢谢:-)

Update: 这对我很有用:

// SEND EMAIL TO AUTHOR OF A CUSTOM POST TYPE ONCE POST IS PUBLISHED

function authorNotification($post_id) {
    if( ( $_POST[\'post_status\'] == \'publish\' ) && ( $_POST[\'original_post_status\'] != \'publish\' ) ) {
 $post = get_post($post_id);
   $author = get_userdata($post->post_author);

   $message = "Hi ".$author->display_name.".
A document for you, ".$post->post_title." has just been published or edited and requires your attention. 
You will be taken to the document after you log in from this link: ".get_permalink( $post_id ).".
   ";
   wp_mail($author->user_email, "An online document has been made or edited for you.", $message);
    }
}

add_action( \'publish_portaldocuments\', \'authorNotification\' );

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

是的,听起来你确实想post transition hook, 可能draft_to_publish 根据法典中的以下内容:

function your_callback( $post ) {
    // Code here
}
add_action( \'draft_to_publish\', \'your_callback\' );
使用authorNotification-- 函数名--而不是your_callback. 你的情况应该很简单。

然而,您的项目的确切细节尚不清楚。你可能需要更复杂的东西。我不知道。

结束

相关推荐

如何在更新USER_EMAIL时更新BILLING_EMAIL

我正在努力实现user_email 和billing_email (woocommerce电子邮件的关键)。到目前为止,我的做法是,当客户编辑/更新其账单电子邮件地址时user_email 使用以下代码更新:add_action( \'woocommerce_customer_save_address\',\'isa_customer_save_address\', 10, 1); function isa_customer_save_address() { globa