While the WordPress Hook system makes WordPress very extendable,
it can sometimes create conflicts with your plugin's functionality.
In this lesson you will learn how to remove hooked callback functions
to prevent them from executing.
Let's say you are developing a plugin that will add some copyright text string
with a date to the end of every page on a WordPress site.
Your plugin code might look something like this.
You receive a support request from a user who is using your plugin,
complaining that there is some other text being added to the end of the page content
that is not related to your plugin.
You investigate and find that another plugin is adding this text to the end of the page content
based on an extra option setting.
You realize that the other plugin is adding the extra text to the end of the page content
using the same filter hook as your plugin, in this case
the 'verkcontent' filter.
The plugin's user wants only the copyright text to be displayed on pages
but to retain the other plugin's functionality on all other post types.
In this situation there is little you can do except to somehow remove
the incompatible callback function hooked into the same filter.
Fortunately WordPress allows you to remove a hooked callback
depending on the hooked type. In the case of a filter
you can use the 'remove_filter' function.
To remove a callback hooked into a filter you call the 'remove_filter' function
passing the filter name and the callback function name to remove.
If the callback function was added with a priority you can also
pass the priority as a third argument.
In this specific case the copyright text should only be displayed on pages
so you should only remove the other plugin's callback function when the content being
registered is actually a page. Ideally you should also
remove any callbacks at the right point in request execution.
In this specific case a good place to remove the callback would be in the
'wp_action_hook' which is executed after the query_post function
which sets up the query loop. Last but not least
this code should be added to a custom plugin or a child_themes_functions.php
file specific to the site.
In the case of an action you can use the 'remove_action' function.
As with removing a filter you call the function passing the action
name and the callback function name to remove.
Again if the callback function was added with a priority you can also pass the priority
as the third argument. One common
usage of remove_action you might see in wordpress plugins
is something like this. This is from the wordcommerce plugin.
It's removing a specific action callback, in this case
the 'woo-commerce-cart-calculate_totals' method,
performing some additional functionality and then adding the action
callback back. This is because the functionality being
performed might trigger the same action again, causing an infinite loop.
In this case the 'callToWC_cart_addToCart'
triggers the 'woo-commerce-addToCart'
action and any callbacks hooked into it.
By removing the specific action callback, performing the functionality
and then adding the action callback back you can prevent this
from happening. It is also possible to remove
all callbacks for a specific hook.
Doing this is not generally recommended, as it can remove important hooks for certain
plugins or themes, and it is better to remove only specific hooks
which create conflicts. If you must remove all
callbacks from an action, you can use the 'removeAllActions' function
and if you must remove all callbacks from a filter
you can use the 'removeAllFilters' function.
For both functions you pass the hook name as the first argument and optionally
the hook priority as the second. It is also important to note
that these functions should not be called from inside the hook you want to remove
the callbacks from, otherwise this would also result in an infinite
loop. For more information around removing hooks
make sure to read the 'removing actions and filters' section
in the 'advanced topics for hooks' of the WordPress plugin developer
handbook.