插件开发文档

💡 云策文档标注

概述

本文档介绍了在WordPress开发中移除动作和过滤器的高级技巧,包括如何正确移除回调函数、处理多个钩子以及调试方法。

关键要点

  • 使用remove_action()或remove_filter()移除已注册的回调函数,参数必须与add_action()或add_filter()完全一致。
  • 移除操作必须在回调函数注册之后执行,执行顺序至关重要。
  • 可以使用remove_all_actions()或remove_all_filters()移除钩子上的所有回调函数。
  • 通过current_action()或current_filter()确定当前钩子,以便在不同钩子上执行不同行为。
  • 使用did_action()检查钩子已运行的次数,以控制回调函数的执行频率。
  • 注册到all钩子可用于调试,帮助追踪事件发生或页面崩溃时机。

代码示例

function wporg_disable_slider() {
    remove_action( 'template_redirect', 'wporg_setup_slider', 9 );
}
add_action( 'after_setup_theme', 'wporg_disable_slider' );

注意事项

  • 移除回调函数时,确保参数匹配,否则移除可能失败。
  • 注意钩子执行顺序,避免在回调函数注册前尝试移除。

📄 原文内容

Removing Actions and Filters

Sometimes you want to remove a callback function from a hook that another plugin, theme or even WordPress Core has registered.

To remove a callback function from a hook, you need to call remove_action() or remove_filter(), depending whether the callback function was added as an Action or a Filter.

The parameters passed to remove_action() / remove_filter() must be identical to the parameters passed to add_action() / add_filter() that registered it, or the removal won’t work.

To successfully remove a callback function you must perform the removal after the callback function was registered. The order of execution is important.

Example

Lets say we want to improve the performance of a large theme by removing unnecessary functionality.

Let’s analyze the theme’s code by looking into functions.php.

function wporg_setup_slider() {
	// ...
}
add_action( 'template_redirect', 'wporg_setup_slider', 9 );

The wporg_setup_slider function is adding a slider that we don’t need, which probably loads a huge CSS file followed by a JavaScript initialization file which uses a custom written library the size of 1MB. We can can get rid of that.

Since we want to hook into WordPress after the wporg_setup_slider callback function was registered (functions.php executed) our best chance would be the <a href="https://developer.wordpress.org/reference/hooks/after_setup_theme/">after_setup_theme</a> hook.

function wporg_disable_slider() {
	// Make sure all parameters match the add_action() call exactly.
	remove_action( 'template_redirect', 'wporg_setup_slider', 9 );
}
// Make sure we call remove_action() after add_action() has been called.
add_action( 'after_setup_theme', 'wporg_disable_slider' );

Removing All Callbacks

You can also remove all of the callback functions associated with a hook by using remove_all_actions() / remove_all_filters().

Determining the Current Hook

Sometimes you want to run an Action or a Filter on multiple hooks, but behave differently based on which one is currently calling it.

You can use the current_action() / current_filter() to determine the current Action / Filter.

function wporg_modify_content( $content ) {
	switch ( current_filter() ) {
		case 'the_content':
			// Do something.
			break;
		case 'the_excerpt':
			// Do something.
			break;
	}
	return $content;
}

add_filter( 'the_content', 'wporg_modify_content' );
add_filter( 'the_excerpt', 'wporg_modify_content' );

Checking How Many Times a Hook Has Run

Some hooks are called multiple times in the course of execution, but you may only want your callback function to run once.

In this situation, you can check how many times the hook has run with the did_action().

function wporg_custom() {
   // If save_post has been run more than once, skip the rest of the code.
   if ( did_action( 'save_post' ) !== 1 ) {
      return;
   }
   // ...
}
add_action( 'save_post', 'wporg_custom' );

Debugging with the “all” Hook

If you want a callback function to fire on every single hook, you can register it to the all hook. Sometimes this is useful in debugging situations to help determine when a particular event is happening or when a page is crashing.

function wporg_debug() {
	echo '<p>' . current_action() . '</p>';
}
add_action( 'all', 'wporg_debug' );