停止每次我更新帖子时发送电子邮件

时间:2017-12-10 作者:Shoeb Mirza

我的草稿帖子将具有自定义字段,其中包含值作为电子邮件地址。因此,每当我发布帖子时,它都会向用户发送电子邮件。它工作得很好。然而,如果我更新帖子,它仍然会一次又一次地发送电子邮件。以下是我的代码:

add_action( \'save_post\', \'send_email\' );
function send_email( $post_id ) {

    if ( !wp_is_post_revision( $post_id ) ) {
        $post_url = get_permalink( $post_id );
        $subject = \'Your post is published!\';
        $message = "Testing!";
        $message .= "<a href=\'". $post_url. "\'>Click here to view</a>";
        $custom_field_name = \'author_email\';
        $email = get_post_meta($post_id, $custom_field_name, true); 
        wp_mail($email, $subject, $message );       
    }
}
我研究了这个主题,这是我在这里找到的解决方案link 但是如何更新代码。任何帮助都将不胜感激!

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

您需要使用{old_status}to{new_status} hook. 和使用draftpublish 状态。只有当您的帖子状态从draftpublish.

function wpse_288250_send_email( $post ) {

    $post_id = $post->ID;

    if( wp_is_post_revision( $post_id ) ) {
        return;
    }

    $post_url = get_permalink( $post_id );
    $subject = \'Your post is published!\';

    $message = "Testing!";
    $message .= "<a href=\'". $post_url. "\'>Click here to view</a>";

    $email = get_post_meta($post_id, \'author_email\', true ); 

    wp_mail($email, $subject, $message );
}

add_action(  \'draft_to_publish\',  \'wpse_288250_send_email\' );
请记住,如果您将职位状态更改回draft 再次发送给publish 将再次发送电子邮件。为了防止出现这种情况,您可以更新post meta,它会告诉您是否已经发送了电子邮件。

function wpse_288250_send_email_once( $post ) {

    $post_id = $post->ID;

    if( wp_is_post_revision( $post_id ) ) {
        return;
    }

    $email_sent = get_post_meta( $post_id, \'email_sent\' );

    if( $email_sent ) {
        return;
    }

    $post_url = get_permalink( $post_id );
    $subject = \'Your post is published!\';

    $message = "Testing!";
    $message .= "<a href=\'". $post_url. "\'>Click here to view</a>";

    $email = get_post_meta($post_id, \'author_email\', true ); 

    wp_mail($email, $subject, $message );
    update_post_meta( $post_id, \'email_sent\', true );
}

add_action(  \'draft_to_publish\',  \'wpse_288250_send_email_once\' );

SO网友:Elex

尝试使用publish_post 钩子代替save_post : https://codex.wordpress.org/Plugin_API/Action_Reference/publish_post : “”publish_post 是一种在发布帖子时触发的操作,或者在编辑帖子并将状态更改为“发布”时触发的操作。“”

Updated response : 让我们尝试一下post状态转换,以确保只有在post进入发布状态时才进行转换。https://codex.wordpress.org/Post_Status_Transitions

function send_email( $new_status, $old_status, $post) {
    if ( $new_status != $old_status && $new_status == "publish") {
        $post_url = get_permalink( $post->ID );
        $subject = \'Your post is published!\';
        $message = "Testing!";
        $message .= "<a href=\'". $post_url. "\'>Click here to view</a>";
        $custom_field_name = \'author_email\';
        $email = get_post_meta($post->ID, $custom_field_name, true); 
        wp_mail($email, $subject, $message );       
    }
}
add_action( \'transition_post_status\', \'send_email\', 10, 3 );

SO网友:Por

add_action( \'save_post\', \'send_email\' );
function send_email( $post_id ) {

    if ( !wp_is_post_revision( $post_id ) && ( get_post_status($post_id) !== \'publish\') ) {
        $post_url = get_permalink( $post_id );
        $subject = \'Your post is published!\';
        $message = "Testing!";
        $message .= "<a href=\'". $post_url. "\'>Click here to view</a>";
        $custom_field_name = \'author_email\';
        $email = get_post_meta($post_id, $custom_field_name, true); 
        wp_mail($email, $subject, $message );       
    }
}
我们可以通过get\\u post\\u状态功能进行检查,不再发送电子邮件。

结束

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在