• Hi all,
    i have found this code for the post

    function notifyAuthorOnPostModification() {
    
      global $post;
    
      $author   = get_user_by( 'id', (int) $post->post_author );
    
      $to       = $author->data->user_email;
    
      $subject  = 'A post of yours has been modified';
    
      $message  = "Your Article '" . $post->post_title . "' (" . get_permalink( $post->ID ) . ') has been modified on ' . date( 'Y-m-d (H:i:s)', time() ); // Or something like that
    
      wp_mail( $to, $subject, $message );
    
    }

    And it Work, but i need the code for the Pages
    There are any solution pls?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The code will work for pages provided the correct action is hooked and 2 parameters are collected. You can use ‘publish_page’ action. If your callback is passed the second parameter as $post, you don’t need global $post;.

    I suggest checking the current user ID against the page author ID before emailing so the original author is not notified of their own edits, only edits by others.

    Thread Starter zambo84

    (@zambo84)

    Ok thx for the answer, i have changed the code

    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;
    	$to = $author ;
    
    	$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( example@example.com, $subject, $message );
    }
    add_action( 'save_post', 'my_project_updated_send_email' );

    This work to,, but i need to send the mail to the Author of the post, can some one explain me the corret code pls?

    Moderator bcworkz

    (@bcworkz)

    This should work, sorry I wasn’t more clear about how to get the $post object.

    function my_project_updated_send_email( $post_id, $post ) {
    
    	// If this is just a revision, don't send the email.
    	if ( wp_is_post_revision( $post_id ) )
    		return;
    
    	$author = get_user_by( 'id', (int) $post->post_author );
    	$to = $author->data->user_email;
    
    	$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 author
    	wp_mail( $to, $subject, $message );
    }
    add_action( 'save_post', 'my_project_updated_send_email', 10, 2 );

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Author notification page’ is closed to new replies.