WordPress hooks are mechanisms that allow developers to inject custom code into various points of the Wordpress core, themes, and plugins. In this article, we will give detailed information about WordPress hooks and their types, i.e. actions and filters.
Related: WooCommerce Pages Hooks: A Comprehensive Guide
Definition
WordPress Hooks are used to add your custom code or modify what WordPress is doing or outputting. There are two types of WordPress hooks.
-
- Actions
-
- Filters
Action
An action is a WordPress hook that is triggered at the specific time when WordPress is running and lets you take the action. It includes things like creating a widget when WordPress is initializing.
Filters
Filters are WordPress hooks that allow modification of data or content before it is displayed or processed. They provide an opportunity to alter values or customize the output.
Add/Remove custom function in WordPress Hooks
The process is simple to hook in your function. For action, you need to know the hook’s name and when it runs. For Filter, you also need to know the hook’s name but 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 are $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 exists 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 the function. Action simply runs the code and doesn’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 );
Examples
There are a lot of hooks that exist in WordPress. Below are a few examples 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 allows you to run the code when the admin view is generated. This is the 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 the above example, we are using the excerpt_length
filter, which returns an integer that determines the length used withthe_excerpt()
.
This article provides a foundational understanding of WordPress hooks, with a focus on actions and filters. As you embark on WordPress development, remember that a plethora of hooks awaits, offering endless possibilities for customization. Explore and experiment with these hooks.
For more in-depth information on WooCommerce-specific hooks, check out our comprehensive guide on WooCommerce Pages Hooks. There are several hooks available for WooCommerce pages.
The post Some useful hooks in WordPress appeared first on The Right Software.