Hook priorities in WordPress: order, negative values and PHP_INT_MAX
Callbacks on the same hook run by priority, from low to high: 5 before 10, 10 before 20, and 10 is the default. That much is known to everybody, but the details cause bugs that are hard to find: the order inside one priority, negative values, running “last”, and removing a callback that was added with another priority.
Problem: A plugin changes the_content, and another plugin’s filter runs later and undoes the change.
Solution: Choose the priority on purpose: a higher number to run later, PHP_INT_MAX to run after everything else, a negative number to run before everything else. To remove another callback, use exactly the priority it was added with.
The basics
<?php add_filter( 'the_content', 'ha_first', 1 ); // runs first add_filter( 'the_content', 'ha_normal' ); // priority 10 add_filter( 'the_content', 'ha_late', 100 ); // after the two above
Inside one priority, callbacks run in the order they were added. Plugins are loaded in alphabetical order of their folders, so two plugins at priority 10 run “alphabetically”, and renaming a plugin can change the order.
Core itself uses the_content with priorities: wptexturize, wpautop and shortcode_unautop at 10, do_shortcode at 11. A filter at 9 sees the content before paragraphs are added, at 12 it sees shortcodes already expanded.
Running last or first
<?php
add_filter( 'the_content', 'ha_final_content_filter', PHP_INT_MAX );
function ha_final_content_filter( $content ) {
return $content . '<p class="ha-note">Thank you for reading.</p>';
}
add_action( 'init', 'ha_very_early_init', -10 ); // before any callback with priority 0 or more
PHP_INT_MAX is a valid priority and the clearest way to say “last”. If two plugins both use it, the order inside it decides again, so do not build a chain of plugins that all want to be last.
Removing a callback
remove_action() and remove_filter() need the same function and the same priority as add_action(). Without a priority they assume 10 and do nothing for a callback added with another one, silently:
<?php
// The parent theme added: add_action( 'wp_head', 'parent_theme_meta', 5 );
add_action( 'after_setup_theme', function () {
remove_action( 'wp_head', 'parent_theme_meta', 5 ); // 5, not the default 10
} );
Remove it after it was added: a child theme’s functions.php runs before the parent’s, so do it on a later hook such as after_setup_theme or init. For scripts, the same idea is shown in enqueueing a script after all other scripts.
Who is attached to a hook
During debugging, list the callbacks and their priorities:
<?php
global $wp_filter;
if ( isset( $wp_filter['the_content'] ) ) {
foreach ( $wp_filter['the_content']->callbacks as $priority => $callbacks ) {
foreach ( $callbacks as $data ) {
$fn = $data['function'];
if ( is_string( $fn ) ) {
$name = $fn;
} elseif ( is_array( $fn ) ) {
$name = ( is_object( $fn[0] ) ? get_class( $fn[0] ) : $fn[0] ) . '::' . $fn[1];
} else {
$name = 'closure';
}
error_log( $priority . ': ' . $name );
}
}
}
Run it late (for example on wp) so all plugins have added their callbacks.
NOTE: Callbacks added as closures cannot be removed with remove_filter(), because you have no reference to the same closure. In code that others might want to change (themes, plugins for distribution), use named functions or methods for your hooks.