How to Pass Arguments to My Function Through Add_Action

Pass parameter to a function hooked with any action or filter hook

To read about hook documentation, see http://codex.wordpress.org/Plugin_API/Filter_Reference and http://codex.wordpress.org/Plugin_API/Action_Reference. These two sites will tell you about the number and type of parameters passed by wordpress when the hook (action or filter) is called.

Or you can look at the wordpress source itself. If you're using an IDE like WebStorm then you can do an Edit->Find->Find in Path and search for the hook name to find examples and the source where the hook is called.

For example, to add a logout action, see function wp_logout() in pluggable.php. The line do_action('wp_logout'); shows you where the action is called and what parameters are passed (in this case, none).

/**
* Log the current user out.
*
* @since 2.5.0
*/
function wp_logout() {
wp_destroy_current_session();
wp_clear_auth_cookie();

/**
* Fires after a user is logged-out.
*
* @since 1.5.0
*/
do_action( 'wp_logout' );
}
endif;

Then, to add a custom action to be called at do_action('wp_logout');, in your theme's functions.php, add this

add_action('wp_logout', 'my_on_logout');

function my_on_logout() {
// do what you want to do
echo 'hi mom';
}

If you see from the do_action or do_filter or from the documentation the hook has parameters, then you specify them in the fourth argument of your add_action or add_filter, like so. The 2 below means that there two parameters to be passed to the publish_product action: ($post_id, $post)

add_action('publish_product', 'my_on_product_update', 10, 2);
**
* do something when a product is created
* @param int $post_id the post id
* @param WP_Post $post not used
*/
function my_on_product_update($post_id, $post) {
// do action
echo 'hi dad';
}

wordpress add_action pass variable

1. Missing value (the answer to your question):

Firstly, the reason your $slider_id isn't available in your anonymous function is because you haven't included it in the closure with a use ($slider_id) like this:

add_action('wp_footer', function($arguments) use ($slider_id) { 
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#" . $slider_id . "').flexslider();
});
</script>";
}, $priority_integer, $accepted_arguments_integer);

2. Using anonymous functions in the add_action() method:

From the WordPress Codex for add_action():

add_action( $tag, $function_to_add, $priority, $accepted_args );

Parameters

...

$function_to_add (callback) (required) The name of the
function you wish to be called. Note: Only string-formatted syntaxes listed in the PHP documentation for the 'callback' type are valid. [my emphasis]

...

Be aware that closures (aka anonymous functions) only work as callbacks in PHP 5.3+, and I (personally) wouldn't rely on them for this as long as the codex states that only string-formatted syntaxes are allowed (even though that same article does later use a closure-based example).

3. Emitting a script:

You should be using register_script and enqueue_script as per this answer at WordPress Answers

(There's a bool parameter to state if script goes in footer or not).

To pass your specific arguments to the script at run-time, use the localize script approach. A tutorial for this is at Otto on Wordpress

Hope this helps.



Related Topics



Leave a reply



Submit