php – Pass argument to event hook

I would suggest first read the wordpress documentation about actions to understand how they work.

Actions are aplit into two section, add_action and do_action

do_action is the “location” where you hook into it.

A basic example of do_action


You can also pass arguments into do_action which can be available in add_action if you choose to use them.

Example for do_action with arguments

do_action('my_custom_action', 'Hello', 'World!');

From the second argument forward every argument can be available in add_action

Now that we have a action we can hook into we can do the following

Basic hook (callback function) without parameters (we use the my_custom_action action)

add_action('my_custom_action', 'my_callback_func');
function my_callback_func () {
    // your login here

Hook with parameters. We know that my_custom_action has two arguments we can use, in order to make them available we can do the following

add_action('my_custom_action', 'my_callback_func', 10, 2);
function my_callback_func ($param1, $param2) {
    echo $param1 . ' ' . $param2;

The output of out callback function will be Hello World!.

I will not go into detail what every part of add_action('my_custom_action', 'my_callback_func', 10, 2) means, wordpress has a great documentaion about this.

Some add_actions will have arguments, some will not, you will need to explore each do_action you want to hook into to see what is available to you to use.

A great plugin that I use constantly when developing in wordpress is Query Monitor, amazing plugin for when you want to understand what action are being used on the current page, what priorety each callback function has, what arguments it uses and so on.

Again, first read the wordpress documentation about hooks, actions and filters, it will give you a much better understating on how they work and how to work with them.