Send email on new post published in WordPress

Here’s a quick snippet for sending an email when a new post is published. It also works for CPT (Custom Post Types).

add_action('future_to_publish', 'send_emails_on_new_event');
add_action('new_to_publish', 'send_emails_on_new_event');
add_action('draft_to_publish' ,'send_emails_on_new_event');
add_action('auto-draft_to_publish' ,'send_emails_on_new_event');


/**
 * Send emails on event publication
 *
 * @param WP_Post $post
 */
function send_emails_on_new_event($post)
{
    $emails = "email_1@mail.com, email_2@mail.com"; //If you want to send to site administrator, use $emails = get_option('admin_email');
    $title = wp_strip_all_tags(get_the_title($post->ID));
    $url = get_permalink($post->ID);
    $message = "Link to post: \n{$url}";

    wp_mail($emails, "New post published: {$title}", $message);
}    

This will result in an email like this:

[Title] New post published: Your Post

Link to post:
http://site.com/your-post/

If you need to do this for a specific post type only you can have a conditional inside the filter function:

...
if(get_post_type($post->ID) === 'page') //post, page, attachment or whatever other CPT you may have
{
    //use wp_mail() here!
}
...

Note: This code was updated on 2014-10-31.

14 thoughts on “Send email on new post published in WordPress

  1. Pingback: Send email when post is sent for review (pending) in WordPress - Useful Snippets

  2. Abdulrahman

    Thank you for this e-mail. We will put it to functions.php of our theme or we will put it in other page???

    Reply
    1. Stanislav Khromov Post author

      You can put this in functions.php of the active theme, it will work. :)

      Reply
  3. Aljun

    Hello.. I think this is a good addition to my plug-in… but how i can put this to my plug-in? that when a user click publish, it will notify me through email? Thank you! Looking forward to your response.

    Reply
    1. Stanislav Khromov Post author

      Just put the above code (first block) in the PHP file for your plugin. :-)

      Reply
  4. Yuriy

    Hi, how to make so that for N minutes before the time of the publication of a pending post (N is set from the admin panel) to the email administrator sends a text message, fill out the same from the admin panel

    Reply
  5. jake

    Trying to get your code to work but i can’t figure out what i am doing wrong..

    add_action(‘future_to_publish’, ‘send_emails_on_new_event’);
    add_action(‘new_to_publish’, ‘send_emails_on_new_event’);
    add_action(‘draft_to_publish’ ,’send_emails_on_new_event’);
    add_action(‘auto-draft_to_publish’ ,’send_emails_on_new_event’);

    /**
    * Send emails on event publication
    *
    * @param WP_Post $post
    */
    function send_emails_on_new_event($post)
    {
    $email = get_post_meta( $post->ID, ’em’, true );
    $orderno = get_post_meta( $post->ID, ‘orderno’, true );
    $name = get_post_meta( $post->ID, ‘trackingname’, true );
    $track = get_post_meta( $post->ID, ‘trackingno’, true );
    $message = “Hello \n{$name}, Thank you for shopping! Your order \n{$orderno} has shipped via UPS and you can track it at https://wwwapps.ups.com/tracking/tracking.cgi?tracknum=\n{$track}. We thank you for your business and visit us at REDACTED if you need anything.”;

    if(get_post_type($post->ID) === ‘tracking’) //post, page, attachment or whatever other CPT you may have
    {

    wp_mail($email, “Your Order {$orderno} has Shipped”, $message);
    }

    }

    Reply
  6. Ketan

    Hello,

    Above code works good for me. But i want to add HTML for on email. so please help me how we can set HTML on that code. I also want to set image on that email.

    So please help me.

    thanks.

    Reply
    1. Stanislav Khromov Post author

      Sorry, that’s outside the scope of this article. Try searching in Google on how to send HTML emails using wp_mail().

      Reply

Leave a Reply

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

Markdown is allowed in comments.