Email the Admin When Someone Else Publishes
Emails the site admin whenever a post or page is published by a user who is not an administrator, so you can review new content as soon as it goes live.
Opens WPIDE on
Code
$wpide_publish_alert_types = array( 'post', 'page' );
add_action( 'transition_post_status', function ( $new, $old, $post ) use ( $wpide_publish_alert_types ) {
if ( 'publish' !== $new || 'publish' === $old || ! in_array( $post->post_type, $wpide_publish_alert_types, true ) || wp_is_post_revision( $post ) ) {
return;
}
$author = get_userdata( (int) $post->post_author );
if ( ! $author || user_can( $author, 'manage_options' ) ) {
return;
}
wp_mail(
get_option( 'admin_email' ),
sprintf( '[%s] New %s by %s: %s', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $post->post_type, $author->display_name, wp_strip_all_tags( get_the_title( $post ) ) ),
sprintf( "%s just published \"%s\".\n\nView: %s\nEdit: %s", $author->display_name, wp_strip_all_tags( get_the_title( $post ) ), get_permalink( $post ), admin_url( 'post.php?post=' . $post->ID . '&action=edit' ) )
);
}, 10, 3 );
Notes
Covers posts and pages by default; scheduled posts trigger the email when they actually go live.
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.