Hooking new functions to actions + passing parameters

I am trying to understand some fundamentals of php with regards to adding new functions to actions. I found a tutorial where he adds a new function to the save_post action…

add_action('save_post', 'log_when_saved'); 

function log_when_saved($post_id){ do something with $post_id };

Is my understanding correct that when we fire the action elsewhere via do_action('save_post', $post_ID, $post, $update) the parameters we pass it at this time are automatically & instantly available to use in our new function we are adding to the action and that is what is going on here?

When we write add_action('save_post', ‘log_when_saved’); we are adding some new function to be run when the action is fired. This new function to be run can automatically use the variable values that were defined when we fired the action via the do_action. Is this correct?

What if we wanted to pass in the $post and $update parameters to this new function also… would we have to do the following…

add_action('save_post', 'log_when_saved'); 

function log_when_saved($post_id, $post, $update){ do something with $post_id, $post, $update};

One of the fundamental things I am trying to understand is do the parameters that we are passing our new function strictly have to be in the order that they were defined in do_action('save_post', $post_ID, $post, $update) and similarly, would you have to call all 3 if we wanted to get the last parameter $update to use in our function?

With regards to naming rules, could we also do the following…

add_action('save_post', 'log_when_saved'); 

function log_when_saved($some_random_variable_name){ do something with $post_id };

and it would know that $some_random_variable_name would be the post id because is first defined argument in our do_action(‘save_post’, $post_ID, $post, $update) statement?

Thank you in advance,

2 Answers
2

For this example lets say we have the following

do_action('bt_custom_action', get_the_ID(), get_the_title(), get_the_content());

The arguments that will be passed to add_action would be in this order

  1. the post id
  2. the post title
  3. the post content

By default if we hook into our do_action without any arguments, like this

add_action('bt_custom_action', 'bt_callback_func');

Our call back function “gets” one argument, in this case the ID

function bt_callback_func ($id) {
    echo $id;
}

In order to get the post content we would need to do something like this

add_action('bt_custom_action', 'bt_callback_func', 10, 3);

This will pass three arguments to our callback function and to use them we would do something like this

function bt_callback_func ($id, $title, $content) {
    echo $id . '<br>' . $title . '<br>' . $content;
}

Now to finally answer your question about getting a specific argument.

If we go by this example

add_action('bt_custom_action', 'bt_callback_func', 10, 3);

we know that our callback function will get three arguments, but we only need the content. We don’t have to set parameters for each and every expected argument. We can use PHPs ..., see Variable-length argument lists.

So now our callback function will look like this

function bt_callback_func (...$args) {
    echo args[2];
}

Because in our add action we told that our callback will expect three arguments and we know that the third argument is what we need (the content), using ... we now created a variable that will contain all passed argumnets.

In this case it will contain an array with three elements in order. ID, title and then content.

We know that content is last, third, in our array. We can target it directly like this $args[2].

Hope this helps =]

Leave a Comment