Reputation: 238
I have created a Wordpress function to display a slideshow in any page. The function works well and I can add a function call to any of my template pages to display a slideshow.
sample function call: add_slideshow($slideshow_id, $gallery_id, $show_navigation_handles);
I have hardcoded the jquery in the footer. However, I want to use add_action('wp_footer','')
in my main function to add the jquery to the footer, because each slideshow may have different settings (different slideshow_id, gallery_id etc).
Here is my function:
function add_slideshow($slider_id, $gal_id, $show_controls){
//get the slideshow images from database and return HTML - A Bit Long to show here but it WORKS!
//add the required script to footer - JavaScript is added to footer but not $slider_id
add_action('wp_footer', function() {
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#" . $slider_id . "').flexslider();
});
</script>";
});
But the PHP variable $slider_id
is not being passed into the emitted javascript, such that the output in my footer is:
<script type='text/javascript'>
$(document).ready(function(){
$('#').flexslider();
});
</script>
When it should be eg this:
<script type='text/javascript'>
$(document).ready(function(){
$('#some_id_here').flexslider();
});
</script>
If I manually add this to my footer with the correct id (#actual_slideshow_id) the slideshow plays OK.
I have searched this site for the last hour to try to find a solution but I couldn't find any that I could adapt to my specific problem.
Thanks, Mark
Upvotes: 0
Views: 5621
Reputation: 4849
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.
Upvotes: 7
Reputation: 56449
1) you need to use global
$myvar ='123';
add_action('blabla','your_func');
function your_func(){
global $myvar;
}
2) OR, use do_action:
- can I pass arguments to my function through add_action?
Upvotes: 0