Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// Copy the lines below this one into a plugin like Code Snippets (run everywhere type)
/**
* Remove an object filter.
*
* @param string $tag Hook name.
* @param string $class Class name. Use 'Closure' for anonymous functions.
* @param string|void $method Method name. Leave empty for anonymous functions.
* @param string|int|void $priority Priority
* @return void
*/
function remove_object_filter( $tag, $class, $method = null, $priority = null ) {
global $wp_version;
$new_wp_filter_struct = false;
$filters = $GLOBALS['wp_filter'][ $tag ];
if ( empty ( $filters ) ) {
return;
}
if ( version_compare( $wp_version, '4.7', '>=' ) && isset( $filters->callbacks ) ) {
$filters = $filters->callbacks;
$new_wp_filter_struct = true;
}
foreach ( $filters as $p => $filter ) {
if ( ! is_null( $priority ) && ( (int) $priority !== (int) $p ) ) {
continue;
}
foreach ( $filter as $identifier => $function ) {
$remove = false;
$function = $function['function'];
if ( $function instanceof Closure && $class === 'Closure' ) {
$remove = true;
}
if ( $remove ) {
if ( $new_wp_filter_struct ) {
unset( $GLOBALS['wp_filter'][ $tag ]->callbacks[ $p ][ $identifier ] );
if ( count($GLOBALS['wp_filter'][ $tag ]->callbacks[ $p ] ) == 0 ) {
unset( $GLOBALS['wp_filter'][ $tag ]->callbacks[ $p ] );
}
} else {
unset( $GLOBALS['wp_filter'][ $tag ][ $p ][ $identifier ] );
}
}
}
}
}
function mepr_remove_closures() {
global $post;
if ( isset( $post->post_type ) && ( MeprProduct::is_product_page( $post ) || MeprUser::is_account_page( $post ) ) ) {
remove_object_filter( 'wp_head', 'Closure', null, 10 );
}
}
add_action( 'wp_head', 'mepr_remove_closures', 9 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment