WordPress Hooks: Actions & Filters

WordPress allows developers to easily add their own custom code in the WordPress core, Themes & Plugins through WordPress hooks. In this article, we will give detailed information about WordPress hooks and their types, i.e actions and filters.


WordPress hooks are used to add your own custom code or modify what the WordPress is doing or outputting. There is two types of hooks in WordPress.


    • Actions

    • Filters

An action is a hook that is triggered at specific time when WordPress is running and let you take the action. It includes things like creating a widget when WordPress is initializing.

A filter is a hook that allows you to get and modify WordPress data before it stores in the database or sent to the browser. Example of filters includes customizing how experts are displayed or adding some custom code to the end of a blog post.

Below are the few methods for troubleshooting the error.

Add/Remove Custom Function

Process is simple to hook in your function. For action, you need to know the name of the hook and when it runs. For Filter, you also need to know the name of the hook but you want to know what value you want to get or have to return. The final bit is a function where you have all your code.

Hook into an Action

add_action( $hook, $function_name, $priority, $accepted_args );

In add_action, the required parameters is $hook, which is the hook name and $function_name, which will be the function name. Priority is the optional integer value, which is from 1 to 999. Higher priority means it will execute later and lower means earlier. The last parameter is used less often. It is for if you need to pass or accept multiple arguments.

Hook into a Filter

add_filter( $tag, $function_name, $priority, $accepted_args );

add_filter works the same way like add_aciton. Sometimes a hook exist as both an action and a filter, or a filter and a function. In parameters $function_name get a value and return at the end of function. Action simply runs the code and don’t return anything.

Unhook from Actions/Filters

Removing a hook is simple. Use remove_action or remove_filter along with the name of the hook, function and priority. The priority is optional and if you have to unhook the function that is hooked more than once and you want to remove a specific occurrence of that function.

remove_action( $tag, $function_to_remove, $priority );

remove_filter( $tag, $function_to_remove, $priority );


There are a lot of hooks that exist in WordPress. Below are the few example of it.

Register a custom Menu in the Admin

function register_my_custom_menu_page() {
 add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'myplugin/myplugin-admin.php', '', 'dashicons-admin-site', 6 );
add_action( 'admin_menu', 'register_my_custom_menu_page' );

In the above example, you can see the function “register_my_custom_menu_page” being hooked into the admin_menu action hook. This allow you to run the code when admin view is generated.  This is most commonly used hook in WordPress.

Change the Excerpt Length

function excerpt_length_example( $words ) {
 return 15;
add_filter( 'excerpt_length', 'excerpt_length_example' );

In above example, we are using the excerpt_length filter, which return a integer which determine the length used with the_excerpt().

This article is a very basic for WordPress Hooks. There are number of hooks available for woocommerce pages.