Search code examples
phpwordpressemailauthor

Wordpress - Send message to users if function_exists by using Front End PM plugin


I'm using the plugin "Front End PM" and i have the snippet below for sending message to the frontend when authors posts is published, but not working for me and the error i get " unexpected '   ' (T_STRING) ", so the problem in between [' '] but i'm sorry i can't fix it.

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.' . "\n\n", $name, $title );
    $message['message_content'] .= sprintf( 'View: %s', $permalink );

    $override = array('post_author' => 1, //change with message sender id
        
    );

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


Solution

  • Edit your snippet in a proper text editor like sublime text, notepad++, Vim (just to name a few). Behind some lines you have characters that are copied from non well formatted code block from a site or from a tool that is not made for writing code. If you remove those characters your code will not generate a syntax error.

    The snippet like this should work:

    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.' . '\n\n', $name, $title );
        $message['message_content'] .= sprintf( 'View: %s', $permalink );
        $override = array('mgs_author' => 1);//change with message sender id  
        
    
        // Send message
        fep_send_message( $message, $override );      
    }
    

    EDIT: post_author was deprecated and removed. Use mgs_author instead.