Tutorial

How to Unhook a Function From an Action in WordPress

The function remove_action unhooks a function from an action in WordPress.

Background

WordPress actions allow the injection of code into a subject to perform custom tasks. An action consists of a name and a set of callbacks. The function do_action triggers an action. This means that WordPress executes all callbacks hooked to the action.

In some cases, a function hooked to an action causes an undesired result when the action is triggered. In other cases, the hooking is unnecessary.

Syntax

remove_action($tag, $function, $priority) removes the function $function from the set of callbacks of the $tag action. $priority is the current priority of $function within the set of callbacks. If $priority is not specified, it takes a value of 10. The result is TRUE if the function was successfully removed, otherwise FALSE.

Keep in mind these tips to avoid common mistakes:

  1. $priority must match the priority specified in the add_action call that hooked the function to the action
  2. remove_action must be executed after the corresponding add_action call

Example

By default, WordPress adds to the <head> element of every web page a <meta> element that indicates the version being executed.

<meta name="generator" content="WordPress X.Y.Z" />

It is considered a security risk to make the WordPress version publicly visible. Best practices recommend hiding this information. This meta tag is generated because the WordPress Core hooks the function wp_generator to the 'wp_head' action, which by convention is triggered at the end of the <head> element.

The solution to this problem is to remove the function wp_generator from the set of callbacks of the 'wp_head' action. Follow this methodology to achieve this purpose. It is adaptable to any action/function pair.

Step 1: Find where the function is hooked to the action in the codebase.

The function add_action hooks a function to an action in WordPress. Open the search feature of the IDE and find occurrences of the text 'wp_generator' in the codebase. Go to the occurrence where the text appears in a call to add_action and 'wp_head' is the target action. You will find that this occurrence is in the file default-filters.php.

Do not forget the single quotes in the search criterion.

add_action('wp_head', 'wp_generator');

Step 2: Unhook the function from the action.

Copy the previous statement to a location executed later. Replace add_action with remove_action.

remove_action('wp_head', 'wp_generator');

Step 3: Test.

Inspect the source code of any web page and verify that a generator meta does not appear in the <head> element.

Further reading

I recommend the other tutorials in this series to learn more about actions in WordPress.

Exercise

If the checkbox “Discourage search engines from indexing this site” of the administrative screen Settings > Reading is enabled, the following element appears in the <head> element of every web page.

<meta name='robots' content='noindex,follow' />

Propose a solution so this meta is never printed, regardless of the configuration set in Settings > Reading.

Source code

The source code developed in this tutorial, including a possible answer to the exercise, is available here.

WordPress

Related Tutorials

Open chat
Need help?
Hi! 🤝 Open the chat if you have any question, feedback, or business proposal. I would love to hear from you.