WordPress hooks allow users to manipulate WordPress without modifying its core. It helps to edit the default settings of themes or plugins and, ultimately, create new functions. The primary purpose of hooks is to automatically run a function and could add or modify features to WordPress without touching its core files.

Hooks are divided into two categories:

  1. Action Hook – Allows users to add data or change how their websites work. An action hook will run at a specific time when the WordPress core, plugins, and themes are executing. To add an action hook, we must activate the add_action () function in a WordPress plugin. To do so, add the following code to your WordPress functions.php file:
add_action( $target_hook, $the_name_of_function_you_want_to_use, $priority, $accepted_args );

2. Filter Hook – Can change data during WordPress core, plugins, and theme execution. To create a filter hook by utilizing the add_filter() function. The filter hook modifies, filters, or replaces a value with a new one. Similar to an action hook, it filters a value with the associated filter hook functions like apply_filter. Here is an example of a filter hook that we will add to the functions.php file for the WordPress theme:

add_filter( 'the_content', 'modify_content' );
function modify_content ( $content ) {
    $content = 'Filter hooks are changed the the last content.';
    return $content;
}

the_content – the target hook that the function will modify.
modify_content – affects the initial value, thus changing the actual content.
$content – replace the content of all your posts with the written phrase.
return $content – returns the new value at the end.

The main difference between an action hook and a filter hook:
  • An action takes the info it receives, does something with it, and returns nothing. In other words: it acts on something and then exits, returning nothing back to the calling hook.
  • A filter takes the info it receives, modifies it somehow, and returns it. In other words: it filters something and passes it back to the hook for further use.

In short, action hooks receive information, act according to it, and don’t return anything to the user. Conversely, the filter hooks get information, modify it, and return it to the user.