Wordpress - 在 Post 发布时发送两条消息,一条给 Post 作者,另一条给所有用户

Wordpress - Send two Messages one for Post Author and one for all Users when the Post is Published

我终于做到了,我正在使用“Front-End PM”插件,到目前为止,我创建了这个函数来为 post 作者发送消息 he/she post 发表。

但是我如何创建两条消息,一条给 post 作者,一条给所有用户,以便通知所有关于新发布的 post.

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

这里是 fep_send_message 函数,我想在一个函数中制作两条消息..这会发生吗?

function fep_send_message( $message = null, $override = array() ) {
    if ( null === $message ) {
        $message = $_POST;
    }
    if ( ! empty( $message['fep_parent_id'] ) ) {
        $message['post_parent'] = absint( $message['fep_parent_id'] );
        $message['post_status'] = fep_get_option( 'reply_post_status', 'publish' );
        $message['message_title'] = __( 'RE:', 'front-end-pm' ). ' ' . wp_slash( fep_get_message_field( 'mgs_title', $message['post_parent'] ) );
        $message['message_to_id'] = fep_get_participants( $message['post_parent'], true );
    } else {
        $message['post_status'] = fep_get_option( 'parent_post_status','publish' );
        $message['post_parent'] = 0;
    }
    $message = apply_filters( 'fep_filter_message_before_send', $message );
    if ( empty( $message['message_title'] ) || empty( $message['message_content'] ) ) {
        return false;
    }
    // Create post array
    $post = array(
        'post_title'    => $message['message_title'],
        'post_content'  => $message['message_content'],
        'post_status'   => $message['post_status'],
        'post_parent'   => $message['post_parent'],
        'post_type'     => 'message',
        'post_author'   => get_current_user_id(),
        'mgs_created'   => current_time( 'mysql', true ),
    );

    if ( $override && is_array( $override ) ) {
        $post = wp_parse_args( $override, $post );
    }
    if( ! $post['post_parent'] && 'threaded' === fep_get_message_view() ){
        $post['mgs_last_reply_by

    '] = $post['post_author'];
            $post['mgs_last_reply_excerpt'] = fep_get_the_excerpt_from_content( 100, $post['post_content'] );
            $post['mgs_last_reply_time'] = $post['mgs_created'];
        }

        $post = apply_filters( 'fep_filter_message_after_override', $post, $message );

        foreach( $post as $k => $v ){
            if( 0 === strpos( $k, 'post_') ){
                $post[ str_replace( 'post_', 'mgs_', $k ) ] = $v;
                unset( $post[ $k ] );
            }
        }
        $post = wp_unslash( $post );

        $new_message = new FEP_Message;
        $message_id = $new_message->insert( $post );
        // Insert the message into the database
        if ( ! $message_id  ) {
            return false;
        }
        /*
        $inserted_message = FEP_Message::get_instance( $message_id );
        if( ! $inserted_message ){
            return false;
        }
        */
        if( ! empty( $message['message_to_id'] ) ){
            $message['message_to_id'] = (array) $message['message_to_id'];
            $message['message_to_id'][] = $new_message->mgs_author;
            $new_message->insert_participants( $message['message_to_id'] );
        }
        do_action( 'fep_action_message_after_send', $message_id, $message, $new_message );

        fep_status_change( 'new', $new_message );

        return $message_id;
    }

请检查以下代码。它会帮助你。

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

function fep_cus_user_publish_send_messaage( $ID, $post ){

    $author = $post->post_author; /* Post author ID. */
    $name = get_the_author_meta( 'display_name', $author );
    $email = get_the_author_meta( 'user_email', $author );
    $title = $post->post_title;
    $permalink = get_permalink( $ID );
    $edit = get_edit_post_link( $ID, '' );
    $to[] = sprintf( '%s <%s>', $name, $email );
    $subject = sprintf( 'Published: %s', $title );
    $messaged = sprintf ('Congratulations, %s! Your article “%s” has been published.' . "\n\n", $name, $title );
    $messaged .= sprintf( 'View: %s', $permalink );
    $headers[] = ''; 
    wp_mail( $to, $subject, $messaged, $headers ); 


    // Send message
    send_message_to_contributor( $name, $title );      
}


function send_message_to_contributor($author_name, $post_title) {

    $blogusers = get_users( [ 'role__in' => [ 'contributor' ] ] );

    foreach ( $blogusers as $user ) {
        $permalink = get_permalink( $ID );
        $user_name = $user->data->display_name;
        $email = $user->data->user_email;
        $to[] = sprintf( '%s <%s>', $user_name, $email );
        $subject = sprintf( 'Published: %s', $post_title );
        $message = sprintf ('Hello, %s! New post is “%s” published.' . "\n\n", $user_name, $post_title );
        $message .= sprintf( 'View: %s', $permalink );
        $headers[] = '';
        wp_mail( $to, $subject, $message, $headers );
    }
}  

如果未发送电子邮件,请安装此插件并设置 SMTP Easy WP SMTP

请检查以下代码以获得 Front End PM 插件。我将 "send_message_to_contributor" 中的一些代码更新为 "fep_send_message"。

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
    send_message_to_contributor( $name, $title );      
}


function send_message_to_contributor( $message = null, $override = array() ) {

    $blogusers = get_users();

    if ( null === $message ) {
        $message = $_POST;
    }
    if ( ! empty( $message['fep_parent_id'] ) ) {
        $message['post_parent'] = absint( $message['fep_parent_id'] );
        $message['post_status'] = fep_get_option( 'reply_post_status', 'publish' );
        $message['message_title'] = __( 'RE:', 'front-end-pm' ). ' ' . wp_slash( fep_get_message_field( 'mgs_title', $message['post_parent'] ) );
        $message['message_to_id'] = fep_get_participants( $message['post_parent'], true );
    } else {
        $message['post_status'] = fep_get_option( 'parent_post_status','publish' );
        $message['post_parent'] = 0;
    }
    $message = apply_filters( 'fep_filter_message_before_send', $message );
    if ( empty( $message['message_title'] ) || empty( $message['message_content'] ) ) {
        return false;
    }
    // Create post array
    $post = array(
        'post_title'    => $message['message_title'],
        'post_content'  => $message['message_content'],
        'post_status'   => $message['post_status'],
        'post_parent'   => $message['post_parent'],
        'post_type'     => 'message',
        'post_author'   => get_current_user_id(),
        'mgs_created'   => current_time( 'mysql', true ),
    );

    if ( $override && is_array( $override ) ) {
        $post = wp_parse_args( $override, $post );
    }
    if( ! $post['post_parent'] && 'threaded' === fep_get_message_view() ){
        $post['mgs_last_reply_by'] = $post['post_author'];
        $post['mgs_last_reply_excerpt'] = fep_get_the_excerpt_from_content( 100, $post['post_content'] );
        $post['mgs_last_reply_time'] = $post['mgs_created'];
    }

    $post = apply_filters( 'fep_filter_message_after_override', $post, $message );

    foreach( $post as $k => $v ){
        if( 0 === strpos( $k, 'post_') ){
            $post[ str_replace( 'post_', 'mgs_', $k ) ] = $v;
            unset( $post[ $k ] );
        }
    }
    $post = wp_unslash( $post );

    $new_message = new FEP_Message;
    $message_id = $new_message->insert( $post );
    // Insert the message into the database
    if ( ! $message_id  ) {
        return false;
    }
    /*
    $inserted_message = FEP_Message::get_instance( $message_id );
    if( ! $inserted_message ){
        return false;
    }
    */
    /*if( ! empty( $message['message_to_id'] ) ){
        $message['message_to_id'] = (array) $message['message_to_id'];
        $message['message_to_id'][] = $new_message->mgs_author;
        $new_message->insert_participants( $message['message_to_id'] );
    }*/
    /*Update code here*/
    foreach ($blogusers as $key => $value) {
        $message['message_to_id'] = array();
        $message_user = $value->data->ID;
        if($message_user != $new_message->mgs_author){
            $message['message_to_id'][] = $message_user;//(array) $message['message_to_id'];
            $message['message_to_id'][] = $new_message->mgs_author;
            $new_message->insert_participants( $message['message_to_id'] );
        }
    }
    /*End updated code*/
    do_action( 'fep_action_message_after_send', $message_id, $message, $new_message );

    fep_status_change( 'new', $new_message );

    return $message_id;
}