Home > Back-end >  Notify when a post is published, with custom messages for Subscribers, Contributors Authors
Notify when a post is published, with custom messages for Subscribers, Contributors Authors

Time:11-22

This would seem useful for many WordPress sites, but I cannot find a full code.

We have a working codesnippet to notify Contributors when their post is published. But as we have User Submitted posts at the front end too, I want a different message to notify Subscribers when their post is published.

Most code I can find is based on the current user, but that responds to who is publishing the post, not who wrote it.

So this is our attempt - with 3 messages. But so far it always sends the final message. Can someone spot the error(s)?

Major thank you.

function notifyauthor($post_id) {

$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
        
//Gets all the data of the author
$authorData = get_userdata( $author );

//checks if the post author has the role of Author or above
    if (in_array( 'author', 'postseditor', 'editor', 'administrator', $authorData->roles)) {

//message for authors in Author role and above
$message = "
Dear ".$author->display_name.",

Your post, \"".$post->post_title."\" has just been published.

View your post: ".get_permalink( $post_id )."

the rest of my message 1"
;   }

    elseif (in_array( 'contributor', $authorData->roles)) {

//message for authors in Contributor role       
$message = "
Dear ".$author->display_name.",

Your post, \"".$post->post_title."\" has just been published.

View your post: ".get_permalink( $post_id )."

the rest of my message 2"
;   }

    else {

//message for authors in Subscriber role
$message = "
Dear ".$author->display_name.",

Your post, \"".$post->post_title."\" has just been published.

View your post: ".get_permalink( $post_id )."

the rest of my message 3"
;  }

   wp_mail($author->user_email, $subject, $message);
} 
add_action('publish_post', 'notifyauthor');

CodePudding user response:

It looks like your comparison function isn't right.

Based on your additional comments, this would send the specified email to the users with specific roles.

function notifyauthor( $post_id ) {

    $post    = get_post( $post_id );
    $author  = get_userdata( $post->post_author );
    $subject = 'Post Published: ' . $post->post_title . '';

    if ( in_array( 'author', (array) $author->roles, true ) ) {
        // message for authors in Author role and above.
        $message = '
        Dear ' . $author->display_name . ',
        Your post, "' . $post->post_title . '" has just been published.
        View your post: ' . get_permalink( $post_id ) . '
        the rest of my message 1';
    } elseif ( in_array( 'contributor', (array) $author->roles, true ) ) {

        // message for authors in Contributor role.
        $message = '
        Dear ' . $author->display_name . ',
        Your post, "' . $post->post_title . '" has just been published.
        View your post: ' . get_permalink( $post_id ) . '
        the rest of my message 2';
    } else {
        // message for authors in Subscriber role.
        $message = '
        Dear ' . $author->display_name . ',
        Your post, "' . $post->post_title . '" has just been published.
        View your post: ' . get_permalink( $post_id ) . '
        the rest of my message 3';
    }

    wp_mail( $author->user_email, $subject, $message );
}

add_action( 'publish_post', 'notifyauthor' );

  • Related