WordPress hooks can help you reach your business goals by expanding your website’s capabilities. Hooks let you interact with and modify code at specific locations without changing the WordPress core itself.You can use WordPress’ many built-in hooks to ‘hook into’ the WordPress Core with your custom code and modify something.
There are two types of WordPress hooks: Actions and Filters. Hooks are so widely used that even WordPress Core makes considerable use of them. In order to allow other developers to hook into your code, WordPress also provides a means for you to construct your own unique custom hooks.
How do WordPress Hooks work?
WordPress hooks are essentially triggers that allow developers to modify or add functionality to different parts of the WordPress codebase. These hooks can be used to completely delete functionality, modify already-existing functionality, or introduce new features. WordPress hooks allow you to ‘hook into’ this build process at certain points and run your custom code. The main function of hooks is to allow you to modify or add features to WordPress without touching the core files.
Action hooks and filter hooks are the two primary categories of hooks in WordPress.
Action hooks allow developers to execute code at specific points in the WordPress execution cycle. They are called “action” hooks because they trigger an action or event within WordPress. For example, the “init” hook is called when WordPress is initializing, while the “wp_footer” hook is called just before the closing </body> tag of a WordPress page.
Here are some common action hooks in WordPress:
- init : This hook is triggered when WordPress is initializing, and is often used to register custom post types, taxonomies, and other functionality.
- wp_head : This hook is called in the head section of a WordPress page, and is often used to add scripts or stylesheets to the page.
- wp_footer : This hook is called just before the closing </body> tag of a WordPress page, and is often used to add scripts or tracking code to the page.
- wp_enqueue_scripts : This hook is called when WordPress is enqueuing scripts and stylesheets, and is often used to add custom scripts or stylesheets to a WordPress site.
- admin_enqueue_scripts : This hook is called when WordPress is enqueuing scripts and stylesheets for the WordPress admin area, and is often used to add custom scripts or stylesheets to the admin area.
To add an action hook, you must activate the add_action () function in a WordPress plugin. To do so, add the following code:
add_action( $target_hook, $the_name_of_function_you_want_to_use, $priority, $accepted_args )
Hooks use a priority scale to function properly. This scale is an automatic ordinal value based on a scale from 1 to 999. It defines the order of functions associated with that particular hook. A lower priority value means the function will run earlier, while the higher one will run later.
Here is an example of using an action hook to add a new menu item to the WordPress admin bar:
In this example, the “wp_before_admin_bar_render” hook is used to add a new menu item to the WordPress admin bar. The “custom_admin_bar_menu” function is called when the hook is triggered, and it adds the new menu item to the admin bar using the $wp_admin_bar->add_menu() method.
Filter hooks allow developers to modify the output or behavior of WordPress functions or templates. They are called “filter” hooks because they filter the output of a WordPress function or template. For example, the “the_title” filter hook is called just before the title of a WordPress post is displayed, allowing developers to modify the title before it is displayed.
Here are some common filter hooks in WordPress:
- the_content: This filter hook is called just before the content of a WordPress post or page is displayed, and can be used to modify the content.
- the_title : This filter hook is called just before the title of a WordPress post or page is displayed, and can be used to modify the title.
- excerpt_length : This filter hook is called when the length of the excerpt for a WordPress post or page is being calculated, and can be used to modify the length of the excerpt.
- the_excerpt : This filter hook is called just before the excerpt of a WordPress post or page is displayed, and can be used to modify the excerpt.
You can 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.
Example of using a filter hook to modify the output of a WordPress function:
In this example, the “excerpt_length” filter hook is used to modify the length of the excerpt for WordPress posts. The “custom_excerpt_length” function is called when the hook is triggered, and it returns a value of 20, which will be used as the new length of the excerpt.
How to Use WordPress Custom Hooks?
Creating your own actions and filters is fairly simple. You use the same functions WordPress Core uses to create hooks. Let’s look at a few examples.
To create custom actions in WordPress use the do_action() function to create a custom action hook. Here’s how you do it:
do_action( ‘ my_custom_action’ )
Now, other developers can hook into your plugin or theme without modifying the source code. All they have to do is register their callback functions to your plugin’s custom action using the add_action() function.
add_action( 'my_custom_action', 'some_callback_function' )
To create a custom filter use the apply_filters() function to create a custom filter hook.
apply_filters( 'my_custom_filter', $value_to_filter )
Your custom filter’s parameters should include a unique identifier and a value to filter. Other developers can hook into your custom filter with the add_filter() function and modify the passed value.
This article serves as a very basic introduction to working with hooks in WordPress. If you would like to go deeper, I suggest checking out WordPress Plugin Handbook for Hooks.