Skip to content

Instantly share code, notes, and snippets.

@fikrirasyid
Created August 28, 2014 15:38
Show Gist options
  • Save fikrirasyid/b90f31825b5e8118e2ed to your computer and use it in GitHub Desktop.
Save fikrirasyid/b90f31825b5e8118e2ed to your computer and use it in GitHub Desktop.
Removing All Actions From Hook
/**
* Unhook all action with some exception on wp_head and wp_footer
*/
function fr_unhook_wp_head_footer(){
global $wp_filter; // Where all the hooks and their actions are stored
// Actions that should not be removed from the wp_head hook
$wp_head_whitelist = array( 'wp_enqueue_scripts', 'wp_print_styles', 'wp_print_head_scripts' );
// Unhook actions from wp_head
foreach ( $wp_filter['wp_head'] as $priority => $wp_head_hooks ) { // Loop the hook. Hook's actions are categorized as multidimensional array by priority
if( is_array( $wp_head_hooks ) ){ // Check if this is an array
foreach ( $wp_head_hooks as $wp_head_hook ) { // Loop the hook
if( !is_array( $wp_head_hook['function'] ) && !in_array( $wp_head_hook['function'], $wp_head_whitelist ) ){ // Check the action against the whitelist
remove_action( 'wp_head', $wp_head_hook['function'], $priority ); // Remove the action from the hook
}
}
}
}
// This is basically a repeatation of what have been done above, only for wp_footer
// Action that should not be removed from wp_footer hook
$wp_footer_whitelist = array( 'wp_print_footer_scripts', 'wc_print_js', 'wp_print_media_templates' );
// Unhook actions from wp_footer
foreach ($wp_filter['wp_footer'] as $priority => $wp_footer_hooks ) {
if( is_array( $wp_footer_hooks ) ){
foreach ( $wp_footer_hooks as $wp_footer_hook ) {
if( !is_array( $wp_footer_hook['function'] ) && !in_array( $wp_footer_hook['function'], $wp_footer_whitelist ) ){
remove_action( 'wp_footer', $wp_footer_hook['function'], $priority );
}
}
}
}
}
add_action( 'wp', 'fr_unhook_wp_head_footer' );
@vladimirlukyanov
Copy link

Not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment