True to my quick tips series, this is a new quick tip! How to add help text above the Publish button on the WordPress admin.
This is a straightforward function. Maybe you want to add a reminder for users before they publish? Maybe you even want to prevent them from publishing based on post meta? If the latter sounds like what you want, then I have a tutorial that can help: How to intercept post publishing based on post meta
If you’re here just to add text above the Publish button, that’s cool too. The snippet is listed below the cut.
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 wp_postmeta.
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.
WordPress Hooks are hard. We always hear the word “hooks” being thrown around and it’s one of those things that make sense, but then it doesn’t.
I’m sure that there may even people reading this right now, and not entirely sure what I mean by hooks. That’s okay. Before I go on with this post, they look like this. These are two examples I pulled from the Twenty Fifteen Theme in functions.php.
Still confused? No worries, it took me a while too. In fact, I can’t say that I completely know all there is to know about WordPress hooks even now, even after writing, “The WordPress Hooks Firing Sequence“. WordPress is the kind of thing that the goal isn’t to learn it all, but to learn it in levels for your purposes. It’s such a deep system, deeper than most people imagine, and I’ve found that once I’ve obtained an understanding of one concept, there is always more to learn. Each concept I learn serves as a foundation for what I learn next.
Hooks didn’t click for me until about a year ago. I too, was copying and pasting various solutions offered by the all knowing Google or Stack Overflow into my WordPress theme. I hadn’t truly understood what was happening when I dropped these snippets of PHP in and the world that would open once that understanding happened.
As much as I’d like to sit here and try to explain what hooks are, if there’s one thing I can say from experience, is that understanding comes in layers. In other words, there is no one mind blowing resource that is suddenly going to make it click. Or maybe it will, depends on the person I guess. For me, however, it took several readings, videos, and diving into WordPress code before the ah hah moment hit.
Knowing that it took a combination of resources and methods for me to learn WordPress hooks, I’d like to pass them onto you. These resources will hopefully bring that fuzzy definition of hooks into focus, even if it’s in layers. In addition to these 10 resources, I’d like to share 2 methods that worked for me. Everyone learns differently and at their own pace, but perhaps sharing my experience will help someone along.
All right, so big questions for WordPress hooks. What hooks exist for use? And what is the WordPress hooks firing sequence?
As in, what order do WordPress hooks fire within their actual context?
There are two resources, the WP Hook Database by Adam Brown and WordPress’ Code Reference to search for hooks. These two are great, but for someone still testing the waters in learning these concepts, it’s pretty overwhelming. Just imagine someone trying to learn the English language and you hand them the English dictionary.
What is all this? There’s just so much here, how do I know what words to use? How much of this do I need? Where do I even begin? And of course, alphabetical order isn’t going to help unless you converse in alphabetical order, right?
The same can be said for the above mentioned resources for hooks. They’re handy if you’re already familiar with hooks and/or if you have a true understanding of how WordPress builds itself in the background.
And so I thought to myself, maybe if I knew what hooks happen all the time in WordPress’ routine processing, and in what order, that might be a good starting point. A piece of the dictionary versus the whole doggone thing. Turns out I’m not the only person thinking this way. There doesn’t seem to be a unified and updated resource to the hook sequence, their definitions, and which ones happen depending on their environment.
So, I figured, if I had this resource, then I can start learning the language effectively – as in, the language of WordPress hooks. If said resource doesn’t exist – maybe it’s time I make one.