Email Authors When Their Post Is Published
Sends the author a short email with the link when one of their posts goes live, whether published by an editor or on schedule.
Opens WPIDE on
Code
add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) {
if ( 'publish' !== $new_status || 'publish' === $old_status || 'post' !== $post->post_type ) {
return;
}
if ( (int) $post->post_author === get_current_user_id() ) {
return;
}
$author = get_userdata( $post->post_author );
if ( ! $author || ! is_email( $author->user_email ) ) {
return;
}
wp_mail(
$author->user_email,
sprintf( 'Your post is live: %s', wp_strip_all_tags( $post->post_title ) ),
sprintf( "Hi %s,\n\nGood news: \"%s\" is now published.\n\n%s\n", $author->display_name, wp_strip_all_tags( $post->post_title ), get_permalink( $post ) )
);
}, 10, 3 );
Notes
Authors are not emailed when they publish their own post.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.