hooks

WordPress Hooks and Filters

What is hooks ?

Hooks in WordPress essentially allow you to change or add code without editing core files. They are very useful for developers.

There are two types of hook: actions and filters.

  • Action Hooks allow you to insert custom code at various points (wherever the hook is run).
  • Filter Hooks allow you to manipulate and return a variable which it passes (for instance a product price).

Note: This is a Developer level documentation. We are unable to provide support for customizations. If you are unfamiliar with code/templates and resolving potential conflicts, select a WordPress Expert or Developer for assistance.

Important Note

Using hooks

To execute your own code, you hook in by using the action hook do_action('action_name');. Here is where to place your code:

If you use a hook to add or manipulate code, you can add your custom code in a variety of ways:

  • To a custom child theme’s functions.php file.
  • Using a plugin such as Code Snippets.
add_action( 'action_name', 'your_function_name' );

function your_function_name() {
// Your code
}

Using filter

Filter hooks are called throughout are code using apply_filter( ‘filter_name’, $variable );. To manipulate the passed variable, you can do something like the following:

add_filter( 'filter_name', 'your_function_name' );

function your_function_name( $variable ) {
// Your code
return $variable;
}
  1. To learn more about what filter and action hooks are, see Plugin API.
  2. To learn about writing plugins in general, see Writing a Plugin.
  3. For a reference list of action hooks, see Plugin API/Action Reference.
  4. For information about filter and action hooks in previous versions of WordPress, see Plugin API/Hooks 2.0.x.
  5. For an automatically-generated list of all WordPress hooks, see the WordPress Hooks Database

Leave a Comment

Your email address will not be published. Required fields are marked *