Skip to content

Instantly share code, notes, and snippets.

@kt-12
Last active December 20, 2018 06:07
Show Gist options
  • Select an option

  • Save kt-12/be7d8d3070dfcbc31f6b755e9c7a7d49 to your computer and use it in GitHub Desktop.

Select an option

Save kt-12/be7d8d3070dfcbc31f6b755e9c7a7d49 to your computer and use it in GitHub Desktop.
WordPress remove_action for class instances - Remove Hook (an action or a filter ) from an instance of the class.
<?php
/**
* Remove a hook ( action or filter ) linked to any instance of the class.
*
* @param string $tag The Hook's name.
* @param string $class_name Name of the class which has the hook.
* @param string $function_to_remove Hook's callback function name.
* @param int $priority Priority of the hook.
* @return boolean returns ture if sucessfully removed.
*/
function remove_action_in_instance( $tag, $class_name, $function_to_remove, $priority = 10 )
{
global $wp_filter;
$removed = false;
if(isset($wp_filter[$tag]->callbacks[$priority]) ){
$hooks = $wp_filter[$tag]->callbacks[$priority];
foreach ($hooks as $key => $hook) {
if( preg_match_all("/[a-z0-9]+". $function_to_remove ."/", $key, $matches) == 1 ){
if ( $hook['function'][0] instanceof $class_name ) {
$removed = remove_filter($tag, array($hook['function'][0], $function_to_remove), $priority);
}
}
}
}
return $removed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment