未加星标

Understanding and Using the WordPress Hook System

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
October 14, 2016 #actions #filters #hooks #wordpress development

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.


Understanding and Using the WordPress Hook System
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 hooks

Actions (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开发工程师 软件开发流程 软件开发工程师

主题: WordHTML
分页:12
转载请注明
本文标题:Understanding and Using the WordPress Hook System
本站链接:http://www.codesec.net/view/485503.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(24)