Understanding and Using the WordPress Hook System
We’ve been covering quite a few WordPress web development topics recently, so you’ve probably already heard about hooks, actions, and filters.
These are an essential part of the using the event-driven architecture used by WordPress. And they are your path to creating your own custom “version” of WordPress to suit your needs.
In a nutshell, an event-driven architecture is a way of listening for events that change a particular state and then reacting accordingly. Your reaction, what you write, is how you can create the functionality you desire to extend the functions of WordPress.
As a very simple example, publishing a WordPress post is an example of an event ( publish_post ) which changes a state.
This is the foundation of the extensibility of WordPress, which has made the CMS such a rich ecosystem, thriving with thousands of feature-rich themes and plugins.
There is another fundamental reason why using WordPress hooks are critical.
Using hooks, you can extend and build upon WordPress without the need to edit any of the core source code.
Actually, let me rephrase that statement.
If anybody you hire for WordPress development (or work for) suggests a core change to the WordPress code, fire them on the spot .
Anybody, who is willing to break the upgradability of your WordPress website has no idea what they are talking about. If they had a grasp on the basics of security, they wouldn’t even think of doing that.
We’ve spoken about this before. Upgrading WordPress (and its themes and plugins) to the latest available version is one of the fundamentalsofsecuring WordPress. Developers who change the core source code will leave your website a sitting duck.
If your hired developers are willing to take such a treacherous path, chances are they are not well-versed in the basics of coding either. It’s very likely they will wreak havoc with the code they write, leaving you (or the people you hire after them) to clean up the mess that they leave behind them.
On the other hand, if you use hooks and all of the other extensibility options WordPress gives you, theupgradability of your WordPress isassured.
That means, whenever a WordPress core update is released, upgrading your WordPress to the latest version won’t break any of your code.You don’t have to worry about the upgrade of your own custom development. As long as the hooks are not deprecated, your customizations will survive the upgrade.
More importantly, your WordPress is kept protected from anyWordPress security vulnerability which has been secured through the latest WordPress core update.
Interested in learning more? The WordPress Codex provides a comprehensive guide to hooks, including a code reference for developers. Hooks, Actions or Filters? What’s What?
Hooks are basically events that happen during the execution of the functionality of WordPress, themes or plugins.
But why are they actually called hooks?
Developers who are looking to extend or implement specific functionality can “hook” onto the event as soon as it happens and perform a specific action related to that event.
As WordPress is going through its execution phases, it “checks” to see whether any plugin (or theme) has registered a function to be executed at that point, and if it finds any, those functions are executed at that point.
The WordPress codex, as part of the documentation of the WordPress API, explains Hooks, Actions and Filters quite nicely, but I’m supplementing this information of course.
There are two main types of hooksActions (aka Action Hooks) Filters (aka Filter hooks)
In reality, these are quite similar in what they can achieve, but there are a few subtle differences.The Difference Between Action Hooks and Filter Hooks
Let’s start with what’s the same before we discuss the differences.
Both actions and filter hooks receive data through a number of parameters.
Actions and filters are also mostly able to do the same thing (if you had to take a look at the WordPress source code, you’ll see that the code functions that implement actions are just wrappers of the filter code functions).
There is though, one key difference between the two types of WordPress hooks.
Filter hooks are required to return a value, action hooks do not.
In essence, with a filter hook, the hooked function gets a specific value, it does its thing and then returns that a modified (or not) version of that value.
An action hook does not need to return a value.
Putting it in more practical terms, with a filter hook, you’re typically working on content, doing your “changes” and then returning that content. For example, as we’ll see below the title_save_pre filter hook works on the post title, whilst the content_save_pre works on the content of the post.
On the other hand, an action hook does something (which can work on data orjust use the data) when an event happens. The publish_post action hook triggers when a post is published.When and How to Use an Action Hook
You should use an action hook if you want to:Inject HTML or other content into the response buffer Modify one or more global variables Modify the parameters passed to your hook function
The way to add an action hook is as follows:
add_action( $hook, $function_to_add, $priority, $accepted_args );
where $hook would be the action you want to hook onto, whilst $function_to_add is the function you’ve written that will be executed when that hook is triggered.
We’ll see some real examples of doing this further on in this article.How and When to Use a Filter Hook
You can use a filter when you need to do any of the above, but you also want / need to change the value of the data parameter.
In fact, it’s not just a want, it’s a requirement. You must return a value for the parameter if you are using a filter hook.
The way to adding a filter hook is quite similar to the way of adding an actionhook:
add_filter( $hook, $function_to_add, $priority, $accepted_args );
Where once again, $hook would be filter hook to use and $function_to_add is the function you’ll be writing.
本文开发（php）相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师
本文标题：Understanding and Using the WordPress Hook System