Sending Out An Email When a Post is Published

A template of the emails you can send using this method

On a recent project, I had created an order tracking system using custom post types, and I needed a way to send out an email to the representative once the order was entered into the system, so that they could then track the order and check for updates at their leisure.

In order to do this, I knew that I needed to hook into the publish_posts action and use the wp_mail function, but I didn’t know quite how to do it, so admittedly I needed a little help from this great article by Smashing Magazine, but I got there in the end. And this is what I came up with.

Firstly, I wanted my emails to be HTML emails, rather than just plain text, so I needed to change that. You can use the change_mail_type filter to do this:

Then I wanted to customize the From name and the From address that the emails generated by WordPress would come from. By default, the From name is WordPress and the From address is, which really didn’t suit this highly customized site. So I used the following function to change it:

Sending out the emails at the right time

Then it was time to write the function that would actually send the email every time an order was created. In my system, I had created a new custom post type called hbos_orders, which is where all the order information was entered, so I needed to make it so that every time a new order was posted, an email was sent to its assigned representative (which I set as the post author).

I wanted to make sure that emails weren’t sent out when a new post or page was published: you can make sure of this by using the action hook for a particular custom post type. For example, publish_posts is the standard action hook for when posts are published (and publish_pages for pages), but if you are using a custom post type, then the action hook becomes publish_custom_post_type (or publish_hbos_orders in this case).

Getting everything needed for wp_mail

wp_mail requires three arguments:

  1. Who the message is going to
  2. What the email subject is
  3. And the content of the message

I’ve already mentioned that I wanted the email to go to the post author, so I retrieved that information with the following snippet:

The subject of the email didn’t need to be dynamic (thought it certainly could have been), so I just used:

Then, using an output buffer, I compiled the simple HTML email using the following template. Notice that it pulls in a few of the order details using both the_title and get_post_meta:

I now have everything I need to use the wp_mail function and send out an email every time an order is created. When I put it all together, I have the following plugin:

As you can see in the last four lines of code, I used the wp_mail function to send the email using the data I had already gathered and then I make that function happen every time an hbos_orders post is published by using the publish_hbos_orders hook.

The possibilities with using these simple functions are really quite endless, so it’s a good place to start if you’re wanting to learn about coding WordPress plugins.

What kind of application did you use this in?

5 thoughts on “Sending Out An Email When a Post is Published”

  1. Flynn says:

    Hey Dave,

    Great post. I used it as the starting point for a site where people select a category of posts they are interested in. Then when a new post is created in that category all members who have selected that category get an email notification.

    Your post was a great help in creating that.

    1. Awesome. If you ever do a write-up of how you achieved that, be sure to leave a link here so others can follow along.

  2. Piet says:

    Hi Dave, excellent post!

    Question about how visible these posts are? Is it safe to assume that not everyone can see those orders? So if it is only showing in the backend, how have you hidden them from the frontend?

    1. On the site, I enabled a plugin which first checked whether a user was logged in. So in this instance, the user will click on a link where they will be asked to log in, if they aren’t already, and then when they log in, they will automatically be forwarded to the correct page sent out in the email.

      1. Piet says:

        Cool, thanks Dave!

Leave a Reply