Наконец-то я это сделал, я использую "Front-End PM" plugin, до сих пор я создал эту функцию для отправки сообщения автору сообщения, когда он / она публикует сообщение.

Но как я могу создать два сообщения: одно для автора сообщения и одно для всех пользователей, чтобы уведомить их всех о новом опубликованном сообщении.

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;
    }
0
Adham Mohamed 18 Сен 2018 в 11:57

2 ответа

Лучший ответ

Пожалуйста, проверьте приведенный ниже код для подключаемого модуля 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;
}
0
Praveen Kumar 9 Окт 2018 в 14:35

Пожалуйста, проверьте ниже код. Это поможет вам.

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

0
Praveen Kumar 9 Окт 2018 в 13:04