Skip to content

Instantly share code, notes, and snippets.

@vdite
Last active March 28, 2024 21:33
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vdite/da7dfe7f59f69e34ae34 to your computer and use it in GitHub Desktop.
Save vdite/da7dfe7f59f69e34ae34 to your computer and use it in GitHub Desktop.
Send an email notification to the Wordpress administrator when a new post is published.
<?php
/**
Plugin Name: Alert Admin on new Post
Plugin URI: http://wordpress.stackexchange.com/questions/19040/alert-email-when-any-post-or-page-is-changed
Description: Send an email notification to the administrator when a new post is published.
Author: TheDeadMedic, transfered by Viktor Dite
Version: 1.0
Copyright CC share alike
* @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 at ' . $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 );
?>
@vdite
Copy link
Author

vdite commented May 27, 2015

Save this file in the plugin folder ** and activate the new plugin for be informed on each new post
** [WORDPRESS FOLDER]/wp-content/plugins/

More Info (in german) at http://webmaster-fragen.de/wordpress-related/wordpress-plugins/wordpress-benachrichtigung-bei-neuen-beitraegen-20150527.html

@jaykumharprajapat
Copy link

how i send mail to user when i publish only new post only

@dviolante
Copy link

Hi, great script!
Any sugestions to make it to work for woocommerce when a new product is published?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment