-
-
Save rodruiz/8a8d8d6d7bf9a13c22606dcb1779c50b to your computer and use it in GitHub Desktop.
Temporarily disabling filters in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Common way to do it: | |
remove_filter( 'the_title', 'wptexturize' ); | |
$title = get_the_title(); | |
add_filter( 'the_title', 'wptexturize' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // What if we could do this: | |
$title = with_filter_disabled( 'the_title', 'wptexturize', function () { | |
return get_the_title(); | |
} ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // What if we could do this: | |
$title = without_filters( 'the_title', function () { | |
return get_the_title(); | |
} ); | |
?> | |
<?php | |
/** | |
* 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; | |
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 ); | |
if ( count( $hook_callbacks ) ) { | |
$wp_filter[ $hook ] = $wp_hook; | |
} | |
return $retval; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment