0

I am using the code below on a multi user blog for sending a email notification to users when new post is published . But the problem is it is sending notification only when a Administrator and Editor is posting. I want to send notification when a Administrator, Editor, Author and Contributor post any thing. So what should I change of the code below for work for this, Please help urgently:

function new_mail_from($old) {
 return 'postmaster@bizcatalyst360.com';
}
function new_mail_from_name($old) {
 return 'Postmaster - BIZCATALYST360°';
}

/** Notification Upon post Pubished */
function authorNotification( $new_status, $old_status, $post ) {
    if ( $new_status == 'publish' && $old_status != 'publish' ) {
        $author = get_userdata($post->post_author);
        $message = "
            Hi ".$author->display_name.",
            New post, ".$post->post_title." has just been published at ".get_permalink(      $post->ID ).". ";
        wp_mail($author->user_email, "$post->post_title", $message);
    }
}
add_action('transition_post_status', 'authorNotification', 10, 3 );
  • I posted an answer but I've deleted it because your question is not clear. What do you want? Notify to post author when the post has been published? (For example, contributor send a post and a editor publish it after revision), or do you want to notify the administrator of the site that someone has posted something, published or not (by default some users can submit posts but can not publish them)? Please, clarify your question. – cybmeta May 14 '14 at 17:39
  • I want that when a Author , Contributor, Editor and Administrator publish a new post then all user will get a notification of the new post. Currently the code given is working when only Administrator and Editor publish but not working when a Contributor and Author is publishing. Thanks. Need urgent help. – facebook-100001191095620 May 14 '14 at 17:43
  • So, do you want to send a notification to all the users on your site about a new post, is it correct?. And please, don't say that your actual code is doing that when administrator or editor publish a post, it is imposible to do that for the given code. – cybmeta May 14 '14 at 17:52
  • Yes. But the given code is doing this for administrator and editor is publishing a new post but not working when other user publish a post. Please urgent help. – facebook-100001191095620 May 14 '14 at 17:54
  • Please, don't say again that the code you posted is sending an email to all the users on your site in some situation, that is not true. – cybmeta May 14 '14 at 17:58
1

Try this. I've tested it and it works although your questions is not so clear (snippet VS description).

function wpkse_144405_on_post_publish( $ID, $post ) {
    $users = get_users( array( 'fields' => array( 'user_email' ) ) );   
    $email_subject = 'New post has been published';
    $message .= 'New post has been published: ' . $post->post_title 
             .  ' You can preview it here: ' . get_permalink( $ID );

    foreach( $users as $user ) {
        wp_mail( $user->user_email, $email_subject, $message );
    }
}
add_action(  'publish_post',  'wpkse_144405_on_post_publish', 10, 2 );

Reference: wp_mail(), Post Status Transitions

1

First of all, @cybmeta is correct. Your code can NOT send your intended notification to ALL of the registered users. It's currently sending the notification ONLY to the author of the post.

Anyway, you can try this:

function send_notification_of_new_post( $post ) {

    $users = get_users( array( 'fields' => array( 'user_email', 'display_name' ) ) );       
    foreach( $users as $user ) {
        wp_mail( $user->user_email, $post->post_title, 'Hi<br>' .$user->display_name.',<br>New post, ' . $post->post_title . ' has just been published at ' . get_permalink( $post->ID ) . '.' );
    }
}
add_action( 'pending_to_publish', 'send_notification_of_new_post', 10, 1 );
add_action( 'draft_to_publish', 'send_notification_of_new_post', 10, 1 );
add_action( 'auto-draft_to_publish', 'send_notification_of_new_post', 10, 1 );
add_action( 'future_to_publish', 'send_notification_of_new_post', 10, 1 );
add_action( 'private_to_publish', 'send_notification_of_new_post', 10, 1 );

This uses Post Status Transitions to determine the status changes and the uses wp_mail() notify all the users if a post changes its status from anything to publish.

1

I only find documentation for _transition_post_status function and it is marked as privte, that means that it is not intended for use by plugin and theme developers. I don't find any documentation for transition_post_status action, the one you are using. Try publish_post action hook instead. See this example, a little modification of an example taken directly from de Codex:

<?php

// SEND EMAIL ONCE POST IS PUBLISHED

function notify_new_post($post_id) {
    if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
         $post = get_post($post_id);
         $email_subject = "A new post has been published.";

         ob_start(); ?>

        <html>
            <head>
                <title>New post at <?php bloginfo( 'name' ) ?></title>
            </head>
            <body>
                <p>
                Hi!!!,
                </p>
                <p>
                There is a new post: <a href="<?php echo get_permalink($post->ID) ?>"><?php the_title_attribute() ?></a>.
                </p>
            </body>
        </html>

        <?php

        $message = ob_get_contents();

        ob_end_clean();

        //Get the email field of all users
        $users = get_users( array( 'fields' => array( 'user_email' ) ) );            
        foreach( $users as $user ) {
             wp_mail( $user->user_email, $email_subject, $message );
        }
    }
}

add_action( 'publish_post', 'notify_new_post' ); ?>

EDIT

I'm now quite familiar with transition_post_status action. The problem you had was with the logic to send the emails, not with the action. The next example is similiat to the above snippet but using transition_post_status action:

add_action('transition_post_status', 'notify_new_post', 10, 3 );
function notify_new_post( $new_status, $old_status, $post ) {
    if ( $new_status == 'publish' && $old_status != 'publish' ) {

       $email_subject = "A new post has been published.";

       ob_start(); ?>

       <html>
         <head>
            <title>New post at <?php bloginfo( 'name' ) ?></title>
         </head>
         <body>
            <p>
            Hi!!!,
            </p>
            <p>
            There is a new post: <a href="<?php echo get_permalink($post->ID) ?>"><?php the_title_attribute() ?></a>.
            </p>
         </body>
       </html>

       <?php

       $message = ob_get_contents();

       ob_end_clean();

        //Get the email field of all users
        $users = get_users( array( 'fields' => array( 'user_email' ) ) );            
        foreach( $users as $user ) {
             wp_mail( $user->user_email, $email_subject, $message );
        }
    }
}
1

Try this its working for me

function authorNotification( $new_status, $old_status, $post ) {
    if ( $new_status == 'publish' && $old_status != 'publish' ) {
        $author = get_userdata($post->post_author);
        $message = "
            Hi ".$author->display_name.",
            New post, ".$post->post_title." has just been published at ".get_permalink( $post->ID ).".
        ";
        wp_mail($author->user_email, "New Post Published", $message);
    }
}
add_action('transition_post_status', 'authorNotification', 10, 3 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.