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 firstname.lastname@example.org, 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:
- Who the message is going to
- What the email subject is
- 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?