wordpress auto send email to admin on page /post update or publish

 //post and page updated -send email to admin

function pnr_auto_send_email( $post_id ) {
global $post;
// If this is just a revision, don’t send the email.
if( $post->post_type == ‘post’){

$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );

if ( $post->post_date != $post->post_modified) {
//update
$subject = ‘A post has been updated’;
}
else{
//published
$subject = ‘A post has been published’;

}

$message = $post->post_type;
$message .= $post_title . “: “ . $post_url;
// Send email to admin.
wp_mail( [email protected], $subject, $message );
}
if( $post->post_type == ‘page’){
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );

if ( $post->post_date != $post->post_modified) {
//update
$subject = ‘A page has been updated’;
}
else{
//published
$subject = ‘A page has been published’;

}

$message = $post->post_type;
$message .= $post_title . “: “ . $post_url;
// Send email to admin.
wp_mail( [email protected], $subject, $message );
}
}

add_action( ‘save_post’, ‘pnr_auto_send_email’ );

shortcode

// // Send email to admin.
// $message = $post->post_type;
// $message .= $post_title . “: ” . $post_url;
// wp_mail( ‘[email protected]’, $subject, $message );

Leave a Comment

Your email address will not be published. Required fields are marked *