Skip to content

Instantly share code, notes, and snippets.

@daronspence
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save daronspence/73e06e037f699ca771dd to your computer and use it in GitHub Desktop.

Select an option

Save daronspence/73e06e037f699ca771dd to your computer and use it in GitHub Desktop.
Remove actions/filters from anonymous class instance
// All of your classes in /acf/forms/ are called anonymously
// so it's near impossible to filter them out without this jargon. :(
// example of won't work, but should
remove_filter('in_widget_form', array('acf_form_widget', 'edit_widget'), 10 );
// See http://wordpress.stackexchange.com/questions/137688/remove-actions-filters-added-via-anonymous-functions
function acfw_remove_object_filter( $tag, $class, $method = NULL, $priority = NULL ) {
$filters = $GLOBALS['wp_filter'][ $tag ];
if ( empty ( $filters ) ) {
return;
}
foreach ( $filters as $p => $filter ) :
if ( ! is_null($priority) && ( (int) $priority !== (int) $p ) ) continue;
$remove = FALSE;
foreach ( $filter as $identifier => $function ) {
$function = $function['function'];
if ( is_array( $function )
&& ( is_a( $function[0], $class ) || ( is_array( $function )
&& $function[0] === $class ) ) ) :
$remove = ( $method && ( $method === $function[1] ) );
elseif ( $function instanceof Closure && $class === 'Closure' ) :
$remove = TRUE;
endif;
if ( $remove ) {
unset( $GLOBALS['wp_filter'][$tag][$p][$identifier] );
}
}
endforeach;
}
@elliotcondon

Copy link
Copy Markdown

What do you think would be a good solution for the issue?

  • Add all the classes to the $acf object as variables. eg: $acf->form_widget
  • Perhaps add them to the $GLOBALS array?

@elliotcondon

Copy link
Copy Markdown

Just had a look through the woocommerce plugin to see if they had a good solution, but they are doing the same as ACF...

@elliotcondon

Copy link
Copy Markdown

I'm guessing this is todo with the widget customizer bug you emailed me about. I'll be reviewing and debugging the problem tomorrow, so hopefully I've got a solution for it shortly.

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