remove_action()WP 1.2.0

Removes a function from a specified action hook.

This can be used to remove default functions attached to a specific action hook and possibly replace them with a substitute.

To remove a hook, the $callback and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.

1 time — 0.00001 sec (speed of light) | 50000 times — 0.06 sec (speed of light)

No Hooks.

Return

true|false. Whether the function is removed.

Usage

remove_action( $hook_name, $callback, $priority );
$hook_name(string) (required)
The action hook to which the function to be removed is hooked.
$callback(callable|string|array) (required)
The name of the function which should be removed. This function can be called unconditionally to speculatively remove a callback that may or may not exist.
$priority(int)
The exact priority used when adding the original action callback.
Default: 10

Examples

0

#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.
0

#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.

0

#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 );
} );
0

#4 Other examples

For more examples, see the function description: remove_filter().

0

#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() code WP 6.4.3

function remove_action( $hook_name, $callback, $priority = 10 ) {
	return remove_filter( $hook_name, $callback, $priority );
}