A template of the emails you can send using this method

Sending Out An Email When a Post is Published

| 5 Comments

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 wordpress@yourdomain.com, 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?

Categories: Code & Snippets | Permalink

What next?

Hire me

If you couldn't quite manage this yourself, find it too intimidating, or just don't have the time to do it, you can always hire Dave to do it. Please get in touch so that we can discuss your needs.

Leave a comment

If you have a question, update, or comment about the tutorial, please leave a comment. I try and respond to every comment, though it may take a few days, so please check back soon.

Let a WordPress Expert help you

Do you want to try this, but feel like you need a helping hand, in case something goes wrong? My service, The WP Butler, gives you access to WordPress expertise whenever you need it. Better yet, I'll keep your site backed up, updated and secure, so that you don't have to worry about it. It's all part of the service. Use coupon DIWW and save 15% on all plans.

Visit The WP Butler

Author:

Dave has been tinkering with WordPress for many years, and he now shares his WordPress knowledge here on Do It WIth WordPress to help others realise its impressive power. He can also be hired to help with your WordPress needs. Dave, who is British, is married to his best friend, Marti, with whom he has a beautiful daughter, Ellie. When he's not dabbling with WordPress, he's probably eating Triscuits or hummus, watching an indie film or British TV show, spending time with friends or family, or exploring the world.

5 Comments

  1. 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.

  2. 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?

Leave a Reply