brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: remove_filter only works when the priority matches the original add_filter

WordPress
~/tips/tip-wordpress-remove-filter-priority-match

add_filter( 'the_title', 'plugin_shout_title', 20 );

Removing a WordPress filter silently fails unless callback and priority match the original.

The most irritating WordPress bugs are the ones that say nothing. remove_filter() is a good example: you call it, nothing happens, and nothing is reported either.

add_filter( 'the_title', 'plugin_shout_title', 20 );

// later, from your own code:
remove_filter( 'the_title', 'plugin_shout_title', 20 );

The docs are strict about this. The callback and the priority must match what was used when the hook was added, and the priority defaults to 10. If the plugin registered the filter at priority 20 and you call remove_filter() without the 20, the callback stays attached. The docs also note that no warning is given on removal failure.

The function returns a bool: whether the function existed before it was removed. That makes it useful for debugging. Wrap it in a check and you know right away if you missed:

if ( ! remove_filter( 'the_title', 'plugin_shout_title', 20 ) ) {
	error_log( 'plugin_shout_title was not attached at priority 20' );
}

Two more things from the reference. The removal has to run after the filter was added, so removing it too early does nothing. And a filter added by a class method needs the class instance, since you pass an array with the object and the method name. For a plugin that keeps its object to itself, that can be a real obstacle. Closures have the same problem: without a reference to the closure you can’t remove it.

The function name, plugin_shout_title, is made up for this example. The same function also removes actions, through the separate remove_action().

Further reading: remove_filter() reference

More WordPress tips curl -s "https://example.com/wp-json/wp/v2/posts/42?_embed" curl -s -D – -o /dev/null "https://example.com/wp-json/wp/v2/posts?per_page=100" curl -s "https://example.com/?rest_route=/" 'You must be logged in.', define( 'AUTOMATIC_UPDATER_DISABLED', true ); All WordPress tips (92) All topics

Link