![Some useful hooks in WordPress](https://therightsw.com/wp-content/uploads/2023/10/wordpress_hooks-1-1.png)
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.
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.
Some useful wordpress hooks are :
wp_head
These action hooks are located in the head section of the WordPress theme. This WordPress hook is used to add additional things like style or script in the header.
add_action(‘wp_head’, ‘callback_function’)
wp_footer
These action hooks are located in the footer section of the WordPress theme. This WordPress hook is used to add script and data that print before closing the body.
add_action(‘wp_footer’, ‘callback_function’)
user_register
user_register is a WordPress action hook that is triggered when a new user is registered on a site.
add_action(‘user_register’, ‘callback_function’)
login_redirect
This WordPress filter hook allows us where redirect users after login.
add_filter(‘login_redirect, ‘callback_function’)
init
This WordPress action hook is triggered after completed WordPress loading but before the header is sent. It’s used to initialize custom functionality like register post type, categories plugin, etc.
add_action(‘init’, ‘callback_function’)
enqueue_script
This WordPress action hook is used to enqueue stylesheet and scripts for WordPress admin and WordPress frontend.
we used the enqueue stylesheet and scripts WordPress admin for the admin panel.
add_action(‘admin_enqueue_script’, ‘callback_function’)
we used enqueue stylesheet and scripts WordPress frontend it for user view.
add_action(‘wp_enqueue_script’, ‘callback_function’)
wp_loaded
This WordPress action hook is triggered after the core initialization is completed.
add_action(‘wp_loaded’, ‘callback_function’)
save_post
This WordPress action hook is used when already already-created post or page is saved.
add_action(‘save_post’, ‘callback_function’
template_redirect
This WordPress action hook is triggered before WordPress action which is the template page load. This hook is used to perform a custom action based requested URL.
add_action(‘template_redirect’, ‘callback_function’)
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 of using WP Hooks
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.
If you need help with any WordPress related issues, you can discuss with a WP experts at The Right Software.