0

I'm setting up jQuery UI on one of my WordPress pages that uses a custom template (course-landing-page.php). I enqueued the scripts for jQuery UI the proper way, but I need to add a JavaScript snippet AFTER the scripts in order to register the tabs on the page.

This is what the code in my functions.php file looks like:

    wp_enqueue_script( 'jquery2', '//code.jquery.com/jquery-1.10.2.js', array( 'jquery' ), '1.10.2', false );

wp_enqueue_script( 'jqueryui', '//code.jquery.com/ui/1.11.4/jquery-ui.js', array( 'jquery' ), '1.11.4', false );


// Add the jQuery 'tabs' code to pages that use the 'course-landing-page.php' template

add_action('wp_enqueue_scripts','jQuery_UI_Tabs');
function jQuery_UI_Tabs(){
    if ( is_page_template('course-landing-page.php') ) {
        echo '<script>
                $(function() {
                    $( "#tabs" ).tabs();
                });
            </script>';

    }

It works great, EXCEPT for the fact that the JavaScript snippet is displaying BEFORE the enqueued jQuery UI scripts, and therefore it's causing the tabs not to work properly. How can I make it so the snippet shows up AFTER the enqueued scripts in the header?

  • 1
    You should place this in it's own javascript file, then use the third parameter of wp_enqueue_script to set the dependencies that this new javascript file depends on – czerspalace May 12 '15 at 22:52
0

Ahh, solved it with this!

add_action( 'wp_head', 'jQuery_UI_Tabs' );
function jQuery_UI_Tabs() {
if ( is_page_template('course-landing-page.php' ) ) {
    echo'<script>
            $(function() {
                $( "#tabs" ).tabs();
            });
        </script>';
} 
  • This is not a good solution. You can use the parameters of wp_enqueue_script to set the dependency and also there is a flag to load it in the footer. – Nick Young May 13 '15 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.