add_action is an extremely used WordPress function, built on top of PHP’s native call_user_func_array function, and widely used in WP core, plugins & themes. In a sense, add_action is a similar method to JavaScript’s on event binding mechanism – on('event').
If you dig into WordPress core files and see do_action('something'), then the ‘something’ is an Action Hook that you can use to do your custom processing using the add_action() function. A list of WordPress hooks (Action hooks) can be found at the Action Reference.
Understanding add_action:
WordPress uses an event binding method, with events such as save_post, post_updated, insert_user & lots more. These are called hooks. Each of these hooks has its own definition (you will find that on the WordPress developer reference pages). These hooks supply one or multiple variables (values). Like, save_post supplies the post_id that is being saved right now, and delete_post supplies the post_id of what will be deleted right now. You use a hook by creating a function and passing the hook name and your function to add_action. Using these hooks, you can do your own processing. Ex: you might need to send an email to the site administrator after a post has been saved, or might want to recalculate the social sharing count after a post has been updated, and much deeper things. I will try to describe a practical example.
An Example of add_action:
Goal: Send an email to a user’s email address upon deleting their account.
Brief: There are two hooks WordPress provides us to use around user account deletion. delete_user – fired before everything about a user is deleted from the database, and deleted_user – fired after all data regarding that user has been deleted from the database. To send the email, we will need to use the first hook delete_user, because on the second hook deleted_user, the user data won’t be available in the database anymore – and we need the user’s email address, which we can only get while the user still exists.
Code example:
add_action('delete_user', 'myplugin_send_email_before_delete_user');
// this is a custom function that is hooked,
// the $user_id variable in our function will be passed by the delete_user hook.
function myplugin_send_email_before_delete_user($user_id)
{
// we are querying the user email from database
$email = get_user_option('user_email', $user_id);
// ofcourse you can retrieve more user information and add into email
wp_mail($email, 'Your account has been deleted', 'The mail content.');
}
So if you put this code in your plugin or your active theme’s functions.php file, an email will be sent to the user whose account has just been deleted. You can test this by creating an account with your email address and deleting it from the WP Admin -> Users page. You will need to login with an administrator account of course.
If you had a question in mind – how do I know that user_id will be supplied to our function (myplugin_send_email_before_delete_user)? The answer is, I read that on the developer reference page. You can read it also – delete_user.
Custom Hook
Besides the hooks WP makes available in the framework, we can create additional hooks for our own purpose. That is done using the do_action() function. Example: We can add a hook to the function we created, myplugin_send_email_before_delete_user, which will allow us or others to add an action on it.
// this is a custom function that is hooked,
// the $user_id variable in our function will be passed by the delete_user hook.
function myplugin_send_email_before_delete_user($user_id)
{
// we are querying the user email from database
$email = get_user_option('user_email', $user_id);
// ofcourse you can retrieve more user information and add into email
wp_mail($email, 'Your account has been deleted', 'The mail content.');
// the new line we just added
do_action('myplugin_user_deletion_notified', $user_id);
}
In the code, the last line means we are allowing anyone to use our custom hook to do something. Just like we have used the save_post hook, we can now also use ‘myplugin_user_deletion_notified’ to do some more things, ex: notify the admin about this deletion, check the next example.
Notify Admin Regarding User Account Deletion
add_action('myplugin_user_deletion_notified', 'myplugin_notify_admin_about_deletion');
// the $user_id variable will be passed by the myplugin_user_deletion_notified hook.
function myplugin_notify_admin_about_deletion($user_id)
{
// we are querying the user name from database
$user_name = get_user_option('display_name', $user_id);
// we are querying the admin email from database
$admin_email = get_option('admin_email');
// send mail
wp_mail(
$admin_email,
'User Account Deleted', // subject
'Hi Admin, An account has been deleted from the site. User name was - '. $user_name // content
);
}
I hope you now understand the concept of hooks and using add_action().

Leave a Reply