Notify admin when page is edited?

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?

Read More

thanks!

Related posts

Leave a Reply

3 comments

  1. 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 );
    
  2. @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:nn";
        $message .= $post_title . ": " . $post_url;
    
        // Send email to admin.
        wp_mail( 'admin@example.com', $subject, $message );
    }
    add_action( 'save_post', 'my_project_updated_send_email' );