Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8578759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:27:55+00:00 2026-06-11T20:27:55+00:00

I have created a WordPress function to display a slideshow in any page. The

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:27:57+00:00Added an answer on June 11, 2026 at 8:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created/modified a function to display breadcrumbs on pages on WordPress. The modified version
I have a WordPress blog on my website. But I've created my own page
I have successfully created a custom taxonomy in WordPress, and I created a page
I have created a simple WordPress plugin that automatically sets my new sites up
I have created a self-hosted wordpress blog and on my ftp server everything is
I have created a searchfrom.php for wordpress but then it's giving me a false
I am very new to WordPress. I have converted a HTML website I created
What kind of WordPress function do you use to show the page contents? I
I have a WordPress plugin that loads pages with AJAX and to ensure compatibility
I have a wordpress theme with an options page. I have included a basic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.