remove_action()
Removes a hook (event or filter). Removes the function attached to the specified hook.
You need to remove the hook after it has been added. This means: if you are removing a hook and it is not being removed, then you are most likely trying to remove it before it is added in the code.
// removing the hook remove_action( 'wp_head', 'my_function', 20 ); // here the included files and somewhere in them this hook // is added like this: add_action( 'wp_head', 'my_function', 20 ); // as a result, your removal does not work!
In this case, you can remove the hook by attaching the removal to another, later hook, for example, wp_loaded:
add_action( 'wp_loaded', function(){
remove_action( 'wp_head', 'my_function');
} );
IMPORTANT: When removing a hook, the priority (parameter $priority) must match the one specified when creating the hook. If they do not match, you will not see any warnings about this - the function will simply return false.
Also read: Hooks in WordPress (removing hooks)
No Hooks.
Returns
true|false. true — the hook was removed. false — the hook was not removed.
Usage
remove_action( $tag, $function_to_remove, $priority );
- $tag(string) (required)
- The name of the filter whose function needs to be removed.
- $function_to_remove(string) (required)
- The name of the function to be removed.
- $priority(number)
- The priority of the hook (function) that was set during addition.
Default: 10
Examples
#1 An example of how to disable an event hook:
Let's un-attach wpautop() function from the_content hook:
remove_action( 'the_content', 'wpautop' );
By the way, You can also disable it with remove_filter() which is an identical function:
remove_filter( 'the_content', 'wpautop' );
remove_action() must be called after the hook was added (the function was attached to the hook). So in most cases this function must be called inside a function and cannot be called directly in your plugin or theme.
add_action( 'wp_head', 'remove_my_action' );
function remove_my_action() {
remove_action( 'the_content', 'wpautop' );
}
Notes:
- You may need to prioritize the removal of the action to a hook that occurs after the action is added.
- You cannot successfully remove the action before it has been added.
- You also cannot remove an action after it has been run.
- To remove an action, the priority must match the priority with the function that was originally added.
#2 Example of deleting an event hook for a PHP class
If the event was added from a class (for example, a plugin added some event), then to remove it you need to know the specific instance of the class that added the hook. Often these class instances are stored in global variables.
Example of deleting a hook (un-attach hook function) called from a class instance of which was saved to the global variable $my_class:
global $my_class; // this is a saved instance of the class remove_action( 'the_content', array( $my_class, 'class_filter_function' ) );
You should keep in mind that a class may not add a hook immediately and you have to delete it after the hook has been added by the class. For this reason you can insert the correct deletion code, but the actual deletion of the hook may not happen.
When you delete a hook that was created within a class, you must pass exactly the instance of the class from which the hook was created.
#3 Removing a hook with a static class method
For static methods we can specify the method for deletion directly.
Suppose a hook was added with this code:
class My_Class {
function __construct(){
// add hook
add_action( 'checkout_init', [ __CLASS__, 'force_login' ], 10, 1 );
}
static function force_login(){
// function code
}
}
new My_Class();
To remove this hook you need: firstly, call the remove function later and secondly, correctly pass the name of the method:
add_action( 'wp_loaded', function(){
remove_action( 'checkout_init', [ 'My_Class', 'force_login' ], 10, 1 );
} ); #4 Other examples
For more examples, see the function description: remove_filter().
#5 Remove an action/filter for un-accessible class object
If you need to be able to remove an action/filter for a class object you do not have access to, you can do so with this function:
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( 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 = [];
}
// Remove this filter from merged_filters, which specifies if filters have been sorted
unset( $GLOBALS['merged_filters'][ $tag ] );
}
return true;
}
}
return false;
}
endif;
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 );
}
endif;
https://gist.github.com/tripflex/c6518efc1753cf2392559866b4bd1a53
Changelog
| Since 1.2.0 | Introduced. |
remove_action() remove action code WP 7.0
function remove_action( $hook_name, $callback, $priority = 10 ) {
return remove_filter( $hook_name, $callback, $priority );
}