How to pass Parameters in WordPress Action hooks such as add_action, do_action
- Article
- Comment
Generally, you can get best docs from the WordPress.org. But for a Specific purpose you need complete tutorial for your work. Here I am going to show the way of passing parameters to wordpress actions hooks. You can’t pass arguments to the add_action, but you can specify the number of parameters you are going to pass the function. Let me start with add_action:
`<?php add_action( $hook, $function_to_add, $priority, $accepted_args ); ?>`
Here $hook is the text, you are going to use for a function
$functions_to_hook, – it may be a single function or array of functions can be grouped here.
$priority – priority to add it to the action queue.
$accepted_args – total number of accepted arguments.
for example,
function kv_my_test_fn_for_add_action($post_id, $category, $post_type) { // here the function which will have three parameters, now we are going to dealt with the function using add_action. }
The next thing is to hook this function to WordPress by the following way
`add_action(‘kv_post_query_function’ , ‘kv_my_test_fn_for_add_action’ , 10, 3 );`
The above hook will add the function to the WordPress actions queue. We need to use the function where we require.
Here is an example for the do_action .
`do_action(‘kv_post_query_function’, $post_id, $category, $post_type);`
Thats it, now your custom action is hooked with WordPress action and called it by using do_action, .Here do action will get all the function parameters.