How do I pass arguments to my hook function?

my_function($arg1, $arg2){
    echo $arg1 . " " . $arg2;
add_action( 'init', 'my_function');

If I want my_function to echo "hello world" for instance, how would I send those parameters with the action hook?

2 Answers 2


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.


According to the documentation of add_action() function, you have to pass the number of parameters for your action with the accepted_args parameter.

This code should be great:

my_function($arg1, $arg2){
    echo $arg1 . " " . $arg2;

add_action( 'init', 'my_function', 10, 2 );

And just call the action like this:

do_action( 'init', 'hello', 'world' );
  • init is a core hook, and manually running it could cause a lot of problems. Commented Jul 12, 2021 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.