Skip to content

Instantly share code, notes, and snippets.

@sabrina-zeidan
Last active September 6, 2022 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sabrina-zeidan/a1e9a016ba469af677fbb394346ec090 to your computer and use it in GitHub Desktop.
Save sabrina-zeidan/a1e9a016ba469af677fbb394346ec090 to your computer and use it in GitHub Desktop.
Remove action added in parent theme in Class [WordPress]
//From here https://gist.github.com/azizultex/dd55c5c0518a427ae3a5552f683eb29b Modified -> Mind the comment to make it work with WP > 4.7
function remove_filters_for_anonymous_class( $hook_name = '', $class_name ='', $method_name = '', $priority = 0 ) {
global $wp_filter;
// Take only filters on right hook name and priority
if ( !isset($wp_filter[$hook_name][$priority]) || !is_array($wp_filter[$hook_name][$priority]) )
return false;
// Loop on filters registered
foreach( (array) $wp_filter[$hook_name][$priority] as $unique_id => $filter_array ) {
// Test if filter is an array ! (always for class/method)
if ( isset($filter_array['function']) && is_array($filter_array['function']) ) {
// Test if object is a class, class and method is equal to param !
if ( is_object($filter_array['function'][0]) && get_class($filter_array['function'][0]) && get_class($filter_array['function'][0]) == $class_name && $filter_array['function'][1] == $method_name ) {
unset( $wp_filter[ $hook_name ]->callbacks[ $priority ][ $unique_id ] );
}
}
}
return false;
}
//after_setup_theme or init to get it work -- as parent theme is called after child theme (we can't remove what hasn't been added yet)
add_action( 'after_setup_theme', 'sz_remove_hook_from_parent_theme');
function sz_remove_hook_from_parent_theme(){
//Stop fonts in the theme, all fonts that need to be preloaded should be added to the related WP Rocket section
remove_filters_for_anonymous_class('wp_head', 'Wpshop\Core\Fonts', 'wp_head_preload_fonts', 10);
//For actions added without class:
// //Stop preloading thumbnail for single posts -- it's not used in the post template, only for archives
remove_action( 'wp_head', 'reboot_preload_thumb_image', 10 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment