Skip to content

Instantly share code, notes, and snippets.

@scarstens
Created March 5, 2015 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scarstens/87cfdf482a2e6b412d84 to your computer and use it in GitHub Desktop.
Save scarstens/87cfdf482a2e6b412d84 to your computer and use it in GitHub Desktop.
remove_action_by_class - Remove actions that are created in WordPress by the add_action hook, but can't be removed with the remove_action hook because an instance of a class created the hook with no way to access it. Use to remove custom actions created by class instances.
<?php
/**
* Function remove_action_by_class
* Used to remove notices and nags or other class actions added with class instances (unable to remove with remove_action)
*
* @param $hook_name
* @param $class_and_function_list
* @param int $priority
*
* ex use case:
* add_action( 'admin_head', array ( $this, 'remove_other_admin_nags' ) );
* function remove_other_admin_nags(){
remove_action_by_class('admin_notices', array('yoast'=>'admin_notices'));
}
*/
function remove_action_by_class($hook_name, $class_and_function_list, $priority = 10) {
global $wp_filter;
// go through manually created class and function list
foreach($class_and_function_list as $class_search => $function_search){
//limit removals to matching action names (wildcard string matching)
foreach($wp_filter[$hook_name][$priority] as $instance => $action){
//limit removals again to matching class and function names (wildcard string matching)
if( stristr($instance,$function_search) && stristr(get_class($action['function'][0]),$class_search) ) {
//action found, removing action from filters
unset($wp_filter[$hook_name][10][$instance]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment