
Essential WordPress Hooks Every Developer Should Know (with Practical Examples) – Part 1
WordPress is the most popular content management system in the world. One big reason for this is its flexibility, which mainly comes from its “Hooks” system. Without Filter Hooks and Action Hooks, WordPress would not be so easy to extend, and we wouldn’t have so many plugins and themes today.
In this tutorial series, I will guide you through everything you need to know about Action Hooks in WordPress.
Using Action Hooks in WordPress
The easiest way to connect a function to a hook is by using the add_action()
function:
<?php
add_action( $hook_name, $function_name, $priority, $arguments );
?>
Here’s what each part means:
$hook_name
: The name of the action hook you want to use.$function_name
: The name of the function you want to run.$priority
: (Optional) A number that decides the order if multiple functions are added to the same hook. Lower numbers run first. Default is 10.$arguments
: (Optional) How many arguments your function accepts.
Here’s a simple example:
<?php
add_action( 'add_meta_boxes', 'my_function', 10, 2 );
function my_function( $post_type, $post ) {
// do stuff with $post_type and $post
}
?>
Remove a Function from an Action Hook
If you want to remove a function that was added to a hook, you can use remove_action()
or remove_all_actions()
.
At the end of the series, we’ll review everything we learned, including the 50 examples covered.
<?php
// remove_action( $hook_name, $function_name, $priority );
remove_action( 'login_enqueue_scripts', 'some_function', 10 );
// remove_all_actions( $hook_name, $priority );
remove_all_actions( 'wp_enqueue_scripts', 10 );
?>
remove_action()
needs the hook name, function name, and the priority.
remove_all_actions()
removes all functions attached to a hook at a given priority.
Create Your Own Action Hook
You can create a hook with do_action()
:
<?php
do_action( $hook_name, $arg1, $arg2, /* ... */ $argN );
/*
* Usage:
*
* add_action( hook_name, my_function )
*
* function my_function( $arg1, $arg2 ) {
* // do stuff with $arg1, $arg2 and so on
* }
*
*/
?>
You can add as many arguments as you need.
If you want to pass arguments as an array, use do_action_ref_array()
instead:
<?php
$args = array( $arg1, $arg2, /* ... */ $argN );
do_action_ref_array( $hook_name, $args );
/*
* Usage:
*
* add_action( hook_name, my_function )
*
* function my_function( $args ) {
* // do stuff with $args[0], $args[1] and so on
* }
*
*/
Check How Many Times an Action Hook Was Called
If you want to know how many times a specific action hook has been run, you can use did_action()
:
<?php
function tuts_foo() {
$action_count = did_action( 'some_action_name' );
if ( 1 === $action_count ) {
// run code if 'some_action_name' has been fired only once
}
}
You just need to provide the action hook’s name as the parameter.
Check if an Action Hook Exists
Sometimes you need to check if a certain hook is available — for example, when making an extension for a popular plugin.
You can use has_action()
to check if a hook exists:
<?php
if( has_action( 'some_popular_plugin_action' ) ) {
add_action( 'some_popular_plugin_action', 'extension_to_the_some_popular_plugin' );
} else {
wp_die( 'Sorry to kill your website but I need "Some Popular Plugin" to work with.' );
// CAUTION: This is a joke. Don't shut down a website because your plugin can't work with it.
}
Just like did_action()
, has_action()
only needs the hook name as the argument.