Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active November 9, 2023 05:22
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save westonruter/6647252 to your computer and use it in GitHub Desktop.
Save westonruter/6647252 to your computer and use it in GitHub Desktop.
Temporarily disabling filters in WordPress
<?php // Common way to do it:
remove_filter( 'the_title', 'wptexturize' );
$title = get_the_title();
add_filter( 'the_title', 'wptexturize' );
<?php // More robust way to do it, but oh so verbose:
$filter_priority = has_filter( 'the_title', 'wptexturize' );
if ( false !== $filter_priority ) {
remove_filter( 'the_title', 'wptexturize', $filter_priority );
}
$title = get_the_title( $post_ID );
if ( false !== $filter_priority ) {
add_filter( 'the_title', 'wptexturize', $filter_priority );
}
<?php // What if we could do this:
$title = with_filter_disabled( 'the_title', 'wptexturize', function () {
return get_the_title();
} );
<?php
/**
* Run $callback with the $handler disabled for the $hook action/filter
* @param string $hook filter name
* @param callable $handler function
* @param callable $callback function execited while filter disabled
* @return mixed value returned by $callback
*/
function with_filter_disabled( $hook, $handler, $callback ) {
$priority = has_filter( $hook, $handler );
if ( false !== $priority ) {
remove_filter( $hook, $handler, $priority );
}
$retval = call_user_func( $callback );
if ( false !== $priority ) {
$accepted_args = PHP_INT_MAX; // for array_slice, can't use null since cast to int
add_filter( $hook, $handler, $priority, $accepted_args );
}
return $retval;
}
@jrevillini
Copy link

Lovin' it. Thank you.

@rodruiz
Copy link

rodruiz commented Aug 3, 2018

Guys, what do you think about this:

$title = without_filters( 'the_title', function () {
	return get_the_title();
} );

/**
 * Run $callback with the $handler disabled for the $hook action/filter
 * @param string $hook filter name
 * @param callable $callback function execited while filter disabled 
 * @return mixed value returned by $callback
 */
function without_filters( $hook, $callback ) {
	global $wp_filter;
	
	$wp_hook = null;
        // Remove and cache the filter
	if ( isset( $wp_filter[ $hook ] ) && $wp_filter[ $hook ] instanceof WP_Hook ) {
		$wp_hook = $wp_filter[ $hook ];
		unset( $wp_filter[ $hook ] );
	}
	
	$retval = call_user_func( $callback );
	
        // Add back the filter
	if ( $wp_hook instanceof WP_Hook  ) {
		$wp_filter[ $hook ] = $wp_hook;
	}
	
	return $retval;
}

@jerclarke
Copy link

Very helpful, thanks. Gonna use a version of this to run AMP_Content with wpautop() and other the_content filters disabled.

@duepi
Copy link

duepi commented May 13, 2020

@rodruiz thanks for the code

i think there's a typo at line
if ( count( $hook_callbacks ) ) {

that must be like
if ( count( $wp_hook ) ) {

btw, thanks again
Pp.

@rodruiz
Copy link

rodruiz commented May 13, 2020

Hi @duepi,
I updated the function and added some comments.

Thanks.

@gregoryfoster
Copy link

Thanks for this! Your code still ranks highly in search results for this particular use case.

I made one amendment to support callback functions which accept an array of arguments ($callback_args):

function with_filter_disabled( $hook, $handler, $callback, $callback_args ) {

...which are just passed along to call_user_func:

$retval = call_user_func( $callback, $callback_args );

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