当前浏览:首页 / WordPress函数 / remove_action()

remove_action( string $hook_name, callable|string|array $callback, int $priority = 10 )

从动作钩子中移除回调函数

action

removemore...


描述

这可以用来移除附加到特定动作挂钩的默认函数,并可能用替代函数替换它们。

要删除钩子,添加钩子时$callback$priority参数必须匹配。这适用于过滤器和动作。移除失败时不会发出警告。


参数

$hook_name

(string)(必填) 将要移除的函数所钩住的动作钩子。

$callback

(callable|string|array)(必填) 要删除的函数的名称。可以无条件调用此函数以推测性地删除可能存在或不存在的回调。

$priority

(int)(可选) 添加原始动作回调时使用的确切优先级。

默认值: 10


返回

(bool) 是否删除该函数。


更多信息

  • 此函数是remove_filter()的别名。
  • 另见add_action()add_filter()
  • 要删除挂钩,添加钩子时$function_to_remove和$priority参数必须匹配。这适用于过滤器和动作。移除失败时不会发出警告。


源码

查看源码 官方文档


更新日志

版本描述
1.2.0开始引入

使用示例

  • 示例1

    此函数与remove_filter()函数相同。

    <?php remove_action( $tag, $function_to_remove, $priority ); ?> 
    

    remove_action()必须在函数内部调用,不能在插件或主题中直接调用。

    <?php
    add_action( 'wp_head', 'remove_my_action' );
    function remove_my_action() {
    	remove_action( 'wp_footer', 'function_being_removed' );
    }
    ?>
    

    如果一个动作是从一个类中添加的,例如通过一个插件添加的,那么移除它将需要通过一个保存该类实例的变量访问该类。

    add_action( 'wp_head', 'remove_my_class_action' );
    function remove_my_class_action() {
    	global $my_class;
    	remove_action( 'wp_footer', array( $my_class, 'class_function_being_removed' ) );
    }
    

    除非函数是静态的,在这种情况下可以直接调用类和函数。

    add_action( 'wp_head', 'remove_my_class_action' );
    function remove_my_class_action() {
    	remove_action( 'wp_footer', array( 'My_Class', 'class_function_being_removed' ) );
    }
    

    笔记:

    1. 您可能需要把移除动作的工作优先安排在动作添加后发生的钩子上。
    2. 在添加动作之前,无法成功删除该动作。
    3. 您也不能在动作运行后删除它。
    4. 要删除动作,优先级必须与最初添加的函数的优先级匹配。
  • 示例2

    如果您需要能够删除您无权访问的类对象的动作/过滤器,可以使用此函数(包括对WordPress 4.7+的支持):

    /**
     * Make sure the function does not exist before defining it
     */
    if( ! function_exists( 'remove_class_filter' ) ){
    	/**
    	 * Remove Class Filter Without Access to Class Object
    	 *
    	 * In order to use the core WordPress remove_filter() on a filter added with the callback
    	 * to a class, you either have to have access to that class object, or it has to be a call
    	 * to a static method.  This method allows you to remove filters with a callback to a class
    	 * you don't have access to.
    	 *
    	 * Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
    	 * Updated 2-27-2017 to use internal WordPress removal for 4.7+ (to prevent PHP warnings output)
    	 *
    	 * @param string $tag         Filter to remove
    	 * @param string $class_name  Class name for the filter's callback
    	 * @param string $method_name Method name for the filter's callback
    	 * @param int    $priority    Priority of the filter (default 10)
    	 *
    	 * @return bool Whether the function is removed.
    	 */
    	function remove_class_filter( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
    		global $wp_filter;
    		// Check that filter actually exists first
    		if ( ! isset( $wp_filter[ $tag ] ) ) {
    			return FALSE;
    		}
    		/**
    		 * If filter config is an object, means we're using WordPress 4.7+ and the config is no longer
    		 * a simple array, rather it is an object that implements the ArrayAccess interface.
    		 *
    		 * To be backwards compatible, we set $callbacks equal to the correct array as a reference (so $wp_filter is updated)
    		 *
    		 * @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/
    		 */
    		if ( is_object( $wp_filter[ $tag ] ) && isset( $wp_filter[ $tag ]->callbacks ) ) {
    			// Create $fob object from filter tag, to use below
    			$fob       = $wp_filter[ $tag ];
    			$callbacks = &$wp_filter[ $tag ]->callbacks;
    		} else {
    			$callbacks = &$wp_filter[ $tag ];
    		}
    		// Exit if there aren't any callbacks for specified priority
    		if ( ! isset( $callbacks[ $priority ] ) || empty( $callbacks[ $priority ] ) ) {
    			return FALSE;
    		}
    		// Loop through each filter for the specified priority, looking for our class & method
    		foreach ( (array) $callbacks[ $priority ] as $filter_id => $filter ) {
    			// Filter should always be an array - array( $this, 'method' ), if not goto next
    			if ( ! isset( $filter['function'] ) || ! is_array( $filter['function'] ) ) {
    				continue;
    			}
    			// If first value in array is not an object, it can't be a class
    			if ( ! is_object( $filter['function'][0] ) ) {
    				continue;
    			}
    			// Method doesn't match the one we're looking for, goto next
    			if ( $filter['function'][1] !== $method_name ) {
    				continue;
    			}
    			// Method matched, now let's check the Class
    			if ( get_class( $filter['function'][0] ) === $class_name ) {
    				// WordPress 4.7+ use core remove_filter() since we found the class object
    				if ( isset( $fob ) ) {
    					// Handles removing filter, reseting callback priority keys mid-iteration, etc.
    					$fob->remove_filter( $tag, $filter['function'], $priority );
    				} else {
    					// Use legacy removal process (pre 4.7)
    					unset( $callbacks[ $priority ][ $filter_id ] );
    					// and if it was the only filter in that priority, unset that priority
    					if ( empty( $callbacks[ $priority ] ) ) {
    						unset( $callbacks[ $priority ] );
    					}
    					// and if the only filter for that tag, set the tag to an empty array
    					if ( empty( $callbacks ) ) {
    						$callbacks = array();
    					}
    					// Remove this filter from merged_filters, which specifies if filters have been sorted
    					unset( $GLOBALS['merged_filters'][ $tag ] );
    				}
    				return TRUE;
    			}
    		}
    		return FALSE;
    	}
    }
    /**
     * Make sure the function does not exist before defining it
     */
    if( ! function_exists( 'remove_class_action') ){
    	/**
    	 * Remove Class Action Without Access to Class Object
    	 *
    	 * In order to use the core WordPress remove_action() on an action added with the callback
    	 * to a class, you either have to have access to that class object, or it has to be a call
    	 * to a static method.  This method allows you to remove actions with a callback to a class
    	 * you don't have access to.
    	 *
    	 * Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
    	 *
    	 * @param string $tag         Action to remove
    	 * @param string $class_name  Class name for the action's callback
    	 * @param string $method_name Method name for the action's callback
    	 * @param int    $priority    Priority of the action (default 10)
    	 *
    	 * @return bool               Whether the function is removed.
    	 */
    	function remove_class_action( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
    		remove_class_filter( $tag, $class_name, $method_name, $priority );
    	}
    }
  • 示例3
  • 示例4
    /**
    * Different way to remove hooks declare inside class
    */
    
    class MyClass {
    	
    	/**
    	 * The single instance of the class.
    	 * 
    	 */
    	protected static $_instance = null;
    
    	/**
    	 * Main plugins instance
    	 *
    	 * Ensures only one instance of this class
    	 * Its always good practice to user single instance of the class so we can modify hooks initialized on this class
    	 */
    	public static function get_instance() {
    		if ( is_null( self::$_instance ) ) {
    			self::$_instance = new self();
    		}
    		return self::$_instance;
    	}
    
    	/**
    	* Register hoooks
    	*/
    	public function __construct() {
    		/*
    		* @hooked add_custom_body_class 10
    		*/
    		add_filter( 'body_class', array( $this, 'add_custom_body_class' ) );
    		/*
    		* @hooked static static_body_class - 20
    		*/
    		add_filter( 'body_class', array( 'MyClass', 'static_body_class' ),  20 );
    	}
    	/**
    	* @return array of class
    	*/
    	public function add_custom_body_class( $class ) {
    		$class[ 'custom_class' ] = 'customClass';
    		return $class;
    	}
    	/**
    	* @return array of class
    	*/
    	static function static_body_class( $class ) {
    		$class[ 'static_custom_class' ] = 'staticCustomClass';
    		return $class;
    	}
    }
    $my_class = MyClass::get_instance();
    
    /**
    * Remove methods: add_custom_body_class attached to body class
    */
    add_action( 'wp_head', 'themeslug_remove_hooks' );
    function themeslug_remove_hooks() {
    	/**
    	* Always use same class object, this can be achieve by restricting multiple instance of the class
    	*/
    	$is_action_removed = remove_action( 'body_class', array( MyClass::get_instance(), 'add_custom_body_class'  ) );
    	/**
    	* Remove static method class by using class name with same priority 20
    	*/
    	$is_action_removed = remove_action( 'body_class', array( 'MyClass', 'static_body_class'  ), 20 );
    }