Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Alert original post author
<?php
function ataylorme_alert_original_author( $post_id ) {
// Skip post revisions
if ( wp_is_post_revision( $post_id ) ) {
return;
}
// Get post data
$post = get_post( $post_id );
$post_type_obj = get_post_type_object( $post->post_type );
$post_type_label = isset( $post_type_obj->labels->singular_name ) ? $post_type_obj->labels->singular_name : $post_type_obj->name;
$post_author = get_userdata( $post->post_author );
// Get last modified by
$last_author_id = get_post_meta( $post_id, '_edit_last', true );
$last_author = get_userdata( $last_author_id );
// Set email subject
$subject = 'Your post has been updated';
// Set email message
$message = sprintf(
"%s updated your %s %s at %s\n\n%s",
esc_html( $last_author->display_name ),
esc_html( strtolower( $post_type_label ) ),
esc_html( $post->post_title ),
esc_html( $post->post_modified ),
get_permalink( $post_id )
);
// Send email
wp_mail( $post_author->user_email, $subject, $message );
}
add_action( 'save_post', 'ataylorme_alert_original_author' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment