Skip to content

Instantly share code, notes, and snippets.

@mlteal
Created April 17, 2018 16:50
Show Gist options
  • Save mlteal/de769e14bb478c6b399a9961aef21b45 to your computer and use it in GitHub Desktop.
Save mlteal/de769e14bb478c6b399a9961aef21b45 to your computer and use it in GitHub Desktop.
<?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
*/
public static function remove_action_by_class( $hook_name, $class_and_function_list, $priority = 10 ) {
global $wp_filter, $wp_version;
$core_version = floatval( $wp_version );
if ( $core_version >= 4.7 ) {
// 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)
// check to make sure the filter we want exists
if ( ! empty( $wp_filter[ $hook_name ]->callbacks[ $priority ] ) ) {
foreach ( $wp_filter[ $hook_name ]->callbacks[ $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 ]->callbacks[ $priority ][ $instance ] );
}
}
}
} // end foreach $class_and_function_list
} else {
// Remove action by hook for versions older than 4.7
// 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)
// check to make sure the filter we want exists
if ( ! empty( $wp_filter[ $hook_name ][ $priority ] ) ) {
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 ][ $priority ][ $instance ] );
}
}
}
} // end foreach $class_and_function_list
} // end version check if statement
} // end remove_action_by_class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment