I had a request for a WordPress plugin that sent an email notification whenever a blog post was put into pending review status. It is amazing that this functionality is not already included in WordPress core.
I did a quick Google search and found this code snippet, it works but I really did not want to make a database call every time it needed to send an email.
I noticed that every time a post is saved I can access that information via the $_POST variable.
$_POST[‘post_status’] holds the post status.
Now I can have a plugin that checks the contents of $_POST[‘post_status’] and if it equals ‘pending’ I know I have a post in pending review status.
Ok, so I built a plugin and it worked, it worked a little too well. I kept recieveing two emails. A little more digging and I discovered that the save_post hook is called twice during each save.
So now I need to add some sort of indicator to the class that keeps track if an email has already been sent. I added a private property to the class $_email_sent_flag this will be set to true once an email is sent.
Now I have everything ready. Except that it sends an email once every minute after being set to preview pending status.
I need to disable auto-save for this to work. A quick google search brought me to this page that had the code to disable auto-save.
Now we have a class that, once in plugin format, will check the post on each save and if it is pending status it will send an email, and set a flag indicating that an email has been sent, so we don’t get multiple emails.
You can grab this class in gist form on Github, I left the exercise of putting this in a plugin for the reader.
Looks nice. What would it take to make this a plugin for the faint-of-heart?