It’s been a while since I’ve written a step by step tutorial. I decided to write one based on a real world task I had to work on.This is a tutorial on how to intercept post publishing in WordPress. In summary, I needed to stop people from publishing posts based on a post meta value. If someone tried to publish a post when they’re not supposed to, from both “quick edit” or regular edit, an error needed to display.
A good use case for this is a news blog where content has to be approved by a head honcho before it gets published. After all, no one wants to publish unvetted content – especially news!
If you have post meta that determines what “stage” the post is in an “approval process” – you can check against that post meta before WordPress decides whether it’s allowed to be published. For those unfamiliar with post meta, I mean any values saved through WordPress’ default custom fields, or if you’re using a field plugin like ACF or Carbon Fields. Post meta can be also be saved via PHP with the add_post_meta() function.
Before we begin, this tutorial assumes:
- You have familiarity with editing/creating WordPress themes.
- You have a basic understanding of how WordPress hooks work. Need some pointers on getting acquainted with them? Check out an older post of mine: 10 Ways to Learn WordPress Hooks.
- You already have a custom field/post meta to work with. In this tutorial, we’re going to assume there is a field called
_approval_status(meta key), and if that field is set to “approved” (meta value), the post should publish successfully. Meta key/value are what the field label/value are called in the WordPress database table
- You have added this field to “quick edit”. Not mandatory, but the tutorial will include how to intercept post publishing on quick edit as well as regular edit.