Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

RSS Latest posts from my personal blog


Most viewed posts


Subscribe to RSS feed


Send email on new post published in WordPress

Stanislav KhromovStanislav Khromov

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.

PHP

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 14
  • Abdulrahman
    Posted on

    Abdulrahman Abdulrahman

    Reply Author

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


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

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


  • Aljun
    Posted on

    Aljun Aljun

    Reply Author

    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.


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

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


      • Aljun
        Posted on

        Aljun Aljun

        Reply Author

        Thank you! It worked :D


  • Yuriy
    Posted on

    Yuriy Yuriy

    Reply Author

    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


  • Yuriy
    Posted on

    Yuriy Yuriy

    Reply Author

    And this is must be a plugin for theme


  • jake
    Posted on

    jake jake

    Reply Author

    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);
    }

    }


  • Ketan
    Posted on

    Ketan Ketan

    Reply Author

    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.


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

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


  • Wilhelm
    Posted on

    Wilhelm Wilhelm

    Reply Author

    Hi

    Can you advice on how to send the mail to the author as well


  • Laura
    Posted on

    Laura Laura

    Reply Author

    That is a very helpful example. It could come in handy for a lot of people.