Skip to content

Instantly share code, notes, and snippets.

@rogden
Created August 6, 2016 18:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rogden/f7b7722a816ab3f5aa23a905653ae642 to your computer and use it in GitHub Desktop.
Save rogden/f7b7722a816ab3f5aa23a905653ae642 to your computer and use it in GitHub Desktop.
Wordpress - Send email new post
<?php
// add to your theme's functions.php file
// _post can be any custom post type as well. I.E. if you have a book post type it would be 'save_post_book'
add_action( 'save_post_post', function( $post_ID, $post, $update ) {
// if it is an update and not a new post, return early
if ( $update ) return;
// a new post was created, send an email out
// send email to this address. You could change this into an array of user emails pulled from the users of
// your site and then loop over calling the below wp_mail for each user. Look into the get_users() function for
// more info: https://codex.wordpress.org/Function_Reference/get_users
$to = 'sendto@theworld.com';
// set your subject. Can include info about post, such as the post_title
$subject = 'New Post from mysite.com - ' . get_the_title( $post_ID );
// let recipient know a new post has been made and include the link to it.
$body = 'A new post has been made on my site! Check it out at ' . get_permalink( $post_ID );
// Set mail content type to text/html if necessay
// if you want to send an HTML email (where the body content above contains html) uncomment the following
//add_filter( 'wp_mail_content_type', function() {
// return 'text/html';
//});
// send the mail.
wp_mail( $to, $subject, $body );
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment