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,