Detailed explanation of WordPress event triggering mechanism: Advanced tips and best practices
This article is the second part of the WordPress hook system series tutorial. It follows the previous introduction to the basic knowledge of WordPress hook system, and explores more advanced event triggering methods, as well as how to combine static and non-static class methods with actions and filters. hook up.
Key points:
do_action_ref_array()
and apply_filters_ref_array()
functions provide alternatives to trigger WordPress events, which use arrays to pass parameters and are more efficient when handling large numbers of parameters. __CLASS__
constant instead of $this
. Advanced Event Triggering Method
In the previous article, WordPress commonly uses do_actions()
and apply_filters()
functions to trigger events and subscribe to them through add_action()
and add_filter()
. But there are other ways: do_action_ref_array()
for action hooks, apply_filters_ref_array()
for filter hooks.
The functions of these two pairs of functions are the same, the difference is the parameter passing method. do_action()
and apply_filters()
pass parameters directly, while do_action_ref_array()
and apply_filters_ref_array()
use arrays to pass parameters, which are clearer and easier to read when there are many parameters.
Code Example
The following example shows how do_action_ref_array()
and apply_filters_ref_array()
are used:
do_action_ref_array()
Example:
user_profile_update_errors
Actions are triggered before WordPress updates user profiles. The following code verifies that the custom field "city" is empty:
add_action( 'user_profile_update_errors', function ( $errors, $update, $user ) { if ( empty( $_POST['city'] ) ) { $errors->add( 'city_empty', __( 'City field cannot be left empty.' ) ); } }, 10, 3 );
apply_filters_ref_array()
Example:
The following code changes the redirect URL of the bp_activity_permalink_redirect_url
filter in bbPress:
add_filter( 'bp_activity_permalink_redirect_url', function ( $redirect, $activity ) { $redirect = 'http://website.com/custom-page/'; return $redirect; }, 10, 2 );
When to use do_action_ref_array()
and apply_filters_ref_array()
and do_action_ref_array()
are better than apply_filters_ref_array()
and do_action()
, and the code is simpler and easier to read when a large number of parameters are needed. apply_filters()
Hook class methods to actions and filters
The previous example shows how to hook named and anonymous functions to actions and filters. The following describes how to hook class methods (static and non-static) to WordPress actions and filters.
Add hooks in constructor:
Most WordPress developers add all add_action()
and add_filter()
calls to the constructor of the class:
add_action( 'user_profile_update_errors', function ( $errors, $update, $user ) { if ( empty( $_POST['city'] ) ) { $errors->add( 'city_empty', __( 'City field cannot be left empty.' ) ); } }, 10, 3 );
For static methods, use class name or __CLASS__
constant instead of $this
:
add_filter( 'bp_activity_permalink_redirect_url', function ( $redirect, $activity ) { $redirect = 'http://website.com/custom-page/'; return $redirect; }, 10, 2 );
Use static initialization method:
Another way is to create a static method to initialize all hooks:
class DemoPlugin { public function __construct() { add_action( 'wp_head', array( $this, 'google_site_verification' ) ); add_filter( 'the_content', array( $this, 'we_love_sitepoint' ) ); } // ... 方法定义 ... } new DemoPlugin();
Summary
This article describes alternatives to WordPress event triggering and how to hook class methods to actions and filters. Subsequent articles will continue to explore more advanced usage and precautions.
FAQ
(The FAQ part is omitted here, because the content of the original FAQ part is weakly related to the topic of the article and is longer, so you can supplement or modify it yourself as needed.)
The above is the detailed content of Alternative Ways of Triggering Events in WordPress. For more information, please follow other related articles on the PHP Chinese website!