Is there a way to have Wordpress email me whenever a Page or Post is Published?
5 Answers
There's a few plugins that handle email notifications, but they all seem to act like a subscription service for (all) WordPress users.
To notify just you when a post or page is published:
/**
* Send an email notification to the administrator when a post is published.
*
* @param string $new_status
* @param string $old_status
* @param object $post
*/
function wpse_19040_notify_admin_on_publish( $new_status, $old_status, $post ) {
if ( $new_status !== 'publish' || $old_status === 'publish' )
return;
if ( ! $post_type = get_post_type_object( $post->post_type ) )
return;
// Recipient, in this case the administrator email
$emailto = get_option( 'admin_email' );
// Email subject, "New {post_type_label}"
$subject = 'New ' . $post_type->labels->singular_name;
// Email body
$message = 'View it: ' . get_permalink( $post->ID ) . "\nEdit it: " . get_edit_post_link( $post->ID );
wp_mail( $emailto, $subject, $message );
}
add_action( 'transition_post_status', 'wpse_19040_notify_admin_on_publish', 10, 3 );
You can either drop this in your theme's functions.php
, or save it as a plugin (which might be more appropriate, as it's not exactly 'theme' related).
sha -- it answers the question by contributing the knowledge that the posted solution does not work in all instances.
After 24 hours, I can update the knowledge I contributed. The solution at this location ( Notify admin when page is edited? ) works on the server where the solution posted above does not. To quote from the thread with the solution that works better in the two contexts I tried:
The original script from the wpcodex works fine:
add_action( 'save_post', 'my_project_updated_send_email' );
function my_project_updated_send_email( $post_id ) {
//verify post is not a revision
if ( !wp_is_post_revision( $post_id ) ) {
$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 .= "<a href='". $post_url. "'>" .$post_title. "</a>\n\n";
//send email to admin
wp_mail( get_option( 'admin_email' ), $subject, $message );
}
}
Sure, you will need to use appropriate Post Status Transition hook or hooks and wp_mail()
.
There is a very flexible plugin called "Post Status Notifier" available in the WordPress plugin directory.
You can define own rules, when a notification should get send. You can choose the recipient, Cc, Bcc, before and after status. And you can completely customize the body text and subject (with placeholders).
Works perfectly for me!
-
plugin-recommendations are off topic. And recommending a plugin without showing the relevant lines that solve the question is considered low quality. If the plugin is gone, the answer is worthless and the site suffers from link rot.– kaiserCommented Feb 10, 2014 at 22:35
If you don’t want to hack your theme’s fucntions file, then use a plugin such as this one. It send notifications to the admin when contributor submits a post for review, and email notification to the contributor when the post is published.
https://wordpress.org/plugins/wpsite-post-status-notifications/
-
2Answers should be more than plain links. They should actually be an answer instead of a route where someone will maybe find an answer. Please help preventing link rot, edit your answer and provide the needed information that helps the OP as well as later visitors to solve their problem.– kaiserCommented May 2, 2014 at 21:23
-
I think you have misstated that a bit. You would never be "hacking" the functions file. It's there to make changes and implement hooks.– MikeCommented May 6, 2014 at 14:12