WordPress hooks

Action hooks

A WordPress hook is a “piece” of code through which a custom function is inserted into the part of the code where the “hook” was previously placed. The hook can be embedded in any part of WordPress, in “core wordpress” or in our theme or plugin…

The execution of wordpress code takes time and has its own sequence, and since the “function-hooks” are located in different places in the code, this means that they are executed at different points in the timeline. This fact allows us to activate our custom function from functions.php at a specific time. The exact start time of our function depends on which function-hook we “hooked” on.

Built-in WP action hooks

Built-in action hooks are divided into several categories:

You can see the list of all WP action-hooks in codex but also on adambrown.info but what is the trigger for
see the function start in Code Reference.

Adding a new action hook

If you still need a “hook” in a special place despite the considerable number of existing action-hooks, this is done by placing the do_action() function in the code.

Where is:

  • $tag the name of the new hook
  • $arg argument passed to the hook (optional)

“Hanging” on a hook

“Hanging” the custom function to the action-hook is done using the function add_action()

  • hook is the hook to which our function
  • is attached

  • $function_to_add represents the name of our function we want to run
    along with action-hook
  • $priority is a number related to the priority of activation, it is used in case there are several custom functions in the same place, ie on the same hook. The smaller the number, the sooner the added function is executed. The default value is 10.
  • $accepted_args represents the number of parameters that come with the function, e.g.function update_ post($a,$b) – here we have number 2 because two parameters are passed

In the following example, we will attach a custom function insert_css_still() to run when the function
wp_head:

The reason we hooked the insert_css_still function with a hook to the wp_head() function,
is that its code is most often placed before </head> taga, and that is also the place where it should be
our function appears.

“Unhooking”

Where is:

  • $tag name “action-hooks”
  • $function_to_remove the name of the custom function associated with the hook to be removed

Filter hooks

“Filter hooks” are functions through which wordpress has made it possible to change data. The filter hook always passes some parameter to our custom function, and it follows that our custom function must always return some value ie. must have a return statement.

Filters can allow data to be changed at the moment before the page is loaded, ie. to make the change after the data has left the database and has not yet reached the browser. Filters also allow data to be changed when the process is reversed, ie. when data goes to the database (eg when new posts or comments are added). That is why it is said that:

” Filter hooks, so-called filters, stand between the database and the browser”

Built-in WP filter hooks

Filter hooks are also divided into categories:

Adding new filter hooks

Where is:

  • $tag name of the new filter hook
  • $value a value or an entire function that can be changed via a filter function
  • $var additional values to be passed (optionally) to a custom function

The list of all filter hooks can be viewed on the website adambrown.info

“Hanging “on a hook

The add_filter() function issues a command to attach a custom function within the plugin or theme to the “filter-hook”. The key difference between an action-hook and a filter-hook is that the former serves to execute some custom functions, while the filter-hook is used to manipulate data using a custom function.

The command issued to the custom function to hook to the “filter hook” is:

  • hook is the function to which our filter
  • is hooked

  • $function_to_add represents the name of our function we want to run
    along with filter-hook
  • $priority is a number related to the priority of activation, it is used in case there are several custom functions in the same place, ie on the same hook. The smaller the number, the sooner the added function is executed. The default value is 10.
  • $accepted_args represents the number of parameters that come with the function, e.g.
    function update_ post($a,$b) – here we have number 2 because two parameters are passed

“Unhooking”

Where is:

  • $tag the name of the “hook to “unhook” from
  • $function_to_remove the name of the custom function to remove.

Example

The previous function returns an “unordered” list of different fruits. So if we add the code to print the function:

The following HTML code will be printed:

If we want to modify the fruit list we need to add a new “filter-hook” inside the code:

With this added code, a new filter-hook named ‘voce_filter_hook’ is created which passes to change $niz_voca as a parameter.

Adding new fruit is now easy as we will create a new custom function adding_fruit_to_list which will change the passed parameter ‘$string_of_fruit’ in place of the filter-hook.