发送电子邮件一次,而不是每次更新一次

时间:2018-11-30 14:21:50

标签: php wordpress email message

我正在使用“ Front End PM”插件,下面有这个代码段,用于在发布其帖子时向该帖子作者发送消息,它工作正常,但每次更新该帖子时仍会发送消息!我怎样才能停止呢?

另一点是如何向所有注册用户发送相同的消息?

add_action( 'publish_post', 'fep_cus_user_publish_send_messaage', 10, 2 );

function fep_cus_user_publish_send_messaage( $ID, $post ){

    if ( ! function_exists( 'fep_send_message' ) )
    return;

    $message = [];

    $message['message_to_id'] = $post->post_author; // Post author ID. 
    $name = get_the_author_meta( 'display_name', $post->post_author );
    $title = $post->post_title;
    $permalink = get_permalink( $ID ); 
    $message['message_title'] = sprintf( 'Published: %s', $title );
    $message['message_content'] = sprintf ('Congratulations, %s! Your article “%s” has been published.', $name, $title );
    $message['message_content'] .= sprintf( 'View: %s', $permalink );
    $message['message_content'] .= sprintf( 'This is an automatic message, to let you know your post is published, and qualified for our quality standard!' );
    $override = array('post_author' => 1);//change with message sender id  


    // Send message
    fep_send_message( $message, $override );  

}

1 个答案:

答案 0 :(得分:1)

使用此方法:

 add_action( 'publish_post', 'fep_cus_user_publish_send_messaage', 10, 2 );

    function fep_cus_user_publish_send_messaage( $ID, $post ){

        if ( ! function_exists( 'fep_send_message' ) )
        return;


    //Check Send
    $send_email = get_post_meta( $post->ID, 'fep_send_email', true );
    if ( ! empty( $send_email ) ) return;


        $message = [];

        $message['message_to_id'] = $post->post_author; // Post author ID. 
        $name = get_the_author_meta( 'display_name', $post->post_author );
        $title = $post->post_title;
        $permalink = get_permalink( $ID ); 
        $message['message_title'] = sprintf( 'Published: %s', $title );
        $message['message_content'] = sprintf ('Congratulations, %s! Your article “%s” has been published.', $name, $title );
        $message['message_content'] .= sprintf( 'View: %s', $permalink );
        $message['message_content'] .= sprintf( 'This is an automatic message, to let you know your post is published, and qualified for our quality standard!' );
        $override = array('post_author' => 1);//change with message sender id  

    //Set Post Meta
    update_post_meta( $post->ID, 'fep_send_email', '1' );

        // Send message
        fep_send_message( $message, $override );  

    }