Last active
December 14, 2015 09:18
-
-
Save stephenharris/5063965 to your computer and use it in GitHub Desktop.
Possible bug with `remove_action()`. An hook is trigged. If a function removes itself from that that hook, all functions added with priority 10 are not triggered * on that specific instance.
This file contains 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 | |
/** | |
* Possible Bug With remove_action()/remove_filter() | |
* | |
* Only been tested with the pre_get_posts hook | |
* | |
* A hook is trigged. If a function removes itself from that that hook, all functions | |
* added with priority 10 are not triggered on that specific instance. | |
* | |
* Below the first function removes itself - it then re-adds itself so that it removes itself | |
* every time the hook is trigged. Consequently the second function is never called. | |
* | |
* Try removing 'add_action'. The second function fires, but it fires 'late' | |
* (i.e. it misses the first instance of pre_get_posts because in that instance remove_myself() | |
* removed itself). | |
* | |
* Try removing 'remove_action()'. Everything works as expected. | |
* | |
* Instead try changing the priority of the second function - works fine... | |
* ...so long as its not between 8-10. | |
* | |
* Does this work for you? Is this a known bug? | |
*/ | |
add_action( 'pre_get_posts', 'remove_myself', 8 ); | |
function remove_myself( $query ) { | |
remove_action( 'pre_get_posts', 'remove_myself', 8 ); | |
add_action( 'pre_get_posts', 'remove_myself', 8 ); | |
return $query; | |
} | |
add_action( 'pre_get_posts', 'never_fired', 10 ); | |
function never_fired(){ | |
echo 'Number of times pre_get_posts has been triggered: ' . did_action( 'pre_get_posts' ); | |
wp_die("I'm never fired"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://betterwp.net/16-remove_filter-and-remove_action-bug/ and http://core.trac.wordpress.org/ticket/9968