3

I found this previous post to automatically notify the admin when a post or page is published: Alert Email when any Post or Page is Changed

Works like a charm, thanks! However, it looks like the page has to be changed from draft or pending to published to trigger the action. Is there a modification to notify the admin when an already published page is updated?

thanks!

1
  • 1
    Hi Pam, welcome to WPSE. Make sure you check out the About and FAQ pages, since this site has some unique mechanics that you'll need to know about.
    – Ian Dunn
    Commented Aug 1, 2012 at 6:28

3 Answers 3

3

You can register TheDeadMedic's function to fire on the save_post action, which runs every time a post is saved, regardless of whether or not the status changed.

add_action( 'save_post', '__notify_admin_on_publish', 10, 3 );

Then, comment out these lines in his function:

//if ( $new_status != 'publish' || $old_status == 'publish' )
        //return;

To prevent getting an e-mail for autosaves, add this code to the top of the function:

global $post;
if( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || $post->post_status == 'auto-draft' )
    return;

Here's the fully merged code:

<?php
function __notify_admin_on_publish( $new_status, $old_status, $post )
{
    global $post;
    if( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || $post->post_status == 'auto-draft' )
        return;

    $message = 'View it: ' . get_permalink( $post->ID ) . "\nEdit it: " . get_edit_post_link( $post->ID );
    if ( $post_type = get_post_type_object( $post->post_type ) )    
        wp_mail( get_option( 'admin_email' ), 'New ' . $post_type->labels->singular_name, $message );
}
add_action( 'save_post', '__notify_admin_on_publish', 10, 3 );
6
  • Thank you, this looks perfect. However, I can't get it to work. I tried putting it in functions.php and then making it a plugin. It doesn't break anything, just no result when I save an update to a page. I also used the admin notification script on the wordpress codex page for save_post [link]codex.wordpress.org/Plugin_API/Action_Reference/save_post, and also had no result. Looks like the save_post action is not triggering for me? thanks
    – pam
    Commented Aug 1, 2012 at 17:29
  • It looks like his function is returning if the post was already published, so I updated my answer to show you how to disable that part of it.
    – Ian Dunn
    Commented Aug 1, 2012 at 18:33
  • Hi! Well, that didn't work, but I managed to modify the script suggested at the WP codex. Unfortunately, it sends a mail every time there is an autosave as well. Code in next post, thanks for your help!
    – pam
    Commented Aug 1, 2012 at 19:47
  • add_action( 'save_post', 'my_project_updated_send_email' ); function my_project_updated_send_email( $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( get_option( 'admin_email' ), $subject, $message ); }
    – pam
    Commented Aug 1, 2012 at 19:48
  • Foolish me, the original script from the wpcodex works fine: 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( get_option( 'admin_email' ), $subject, $message ); } }
    – pam
    Commented Aug 1, 2012 at 19:54
2

You're right .. many of the notification plugins require a change of state and don't actually track if there is a new 'revision' to a post or page.

Recently WPMU did a long story on plugins that will track activity and you might find it helpful as it identifies a few plugins ...

http://wpmu.org/wordpress-activity-log/

1
  • Thank you, I just reviewed that story. I had looked at most of those plugins, and they don't appear to be the answer for me, as they log but don't notify. I'd like to be warned immediately that changes are being made, not just be able to trace it after the fact. My site isn't really multi-user, but I do need to delegate editing authority to a few other staff and would like to keep an eye on changes.
    – pam
    Commented Aug 1, 2012 at 17:32
1

@IanDunn's answer should work but I haven't tested it. However the structure for save_post callbacks is different from transition_post_status. The WordPress codex save_post page has its own code for save notifications:

<?php
function my_project_updated_send_email( $post_id ) {

    // If this is just a revision, don't send the email.
    if ( wp_is_post_revision( $post_id ) ) {
        return;
    }

    $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 .= $post_title . ": " . $post_url;

    // Send email to admin.
    wp_mail( '[email protected]', $subject, $message );
}
add_action( 'save_post', 'my_project_updated_send_email' ); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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