Skip to content

Instantly share code, notes, and snippets.

@castroalves
Created August 6, 2013 18:38
Show Gist options
  • Save castroalves/6167297 to your computer and use it in GitHub Desktop.
Save castroalves/6167297 to your computer and use it in GitHub Desktop.
WordPress Hook to Notify Users When New Post Was Published
/**
* Send E-mails When a New Post is Published
*/
function email_notify( $new_status, $old_status, $post ) {
// Avoids re-sending e-mails on update
if( $new_status == 'publish' && $new_status != $old_status ) {
$message = 'The post ' . $post->post_title . ' was published at ' . get_bloginfo('name');
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
wp_mail( 'recipient@email.com', 'New Post Published At ' . get_bloginfo('name'), $message );
// Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
return $post->ID;
}
}
add_action( 'transition_post_status', 'email_notify', 10, 3 );
function set_html_content_type() {
return 'text/html';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment