0

I've found this code that worked perfectly for notifying admin when a post is updated, but it's annoying the admin because she's getting emails for all updates and she only wants notifications for posts that are pending. I cannot figure out how to do this.

Here's my code so far:

add_action( 'save_post', 'my_project_updated_send_email' ); 
function my_project_updated_send_email( $post_id ) { 
    //verify post is not a revision 
    if ( !wp_is_post_revision( $post_id ) ) { 
        $post_title = get_the_title( $post_id ); 
        $post_url = get_permalink( $post_id ); 
        $subject = 'A post has been updated'; 
        $message = "A post has been updated on your website:\n\n";
        $message .= "<a href='". $post_url. "'>" .$post_title. "</a>\n\n"; 
        //send email to admin 
        wp_mail( 'amdin@test.co.uk', $subject, $message ); 
    } 
} 

I've tried changing add_action('save_post'

to add_action('on_publish_pending_post'

But that didn't work. I tried just 'pending', but that didn't work. I'm a bit stuck.

0

I did a bit more research and this is what is working for me now.

The admin will only get an update if a pending post is added or updated.

add_action( 'transition_post_status', 'pending_post_status', 10, 3 );

function pending_post_status( $new_status, $old_status, $post ) {

  if ( $new_status === "pending" ) {

    $post_title = get_the_title( $post_id ); 
     $post_url = get_permalink( $post_id ); 
     $subject = 'A post has been updated'; 
     $message = "A post has been updated on your website:\n\n";
     $message .= "<a href='". $post_url. "'>" .$post_title. "</a>\n\n"; 
     //send email to admin 

     wp_mail( 'admin@test.co.uk' , $subject, $message ); 
  }

}

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.