• Resolved CelticPride21

    (@celticpride21)


    I was successfully able to load my scripts through the ‘functions.php’ file thanks to this post:

    http://ww.wp.xz.cn/support/topic/someone-please-help-me-with-adding-a-jquery-script-to-wordpress
    moderator note: a note has been added to that post that deregistering jQuery is never a good idea

    Unfortunately I’m not done just yet. I was planning on using this script for creating a responsive menu but noticed that it requires an initializer in order for it to load. The traditional method in their tutorial was to put it at the bottom of your HTML file before the closing of your body tag. What would be the correct way in doing so for WP?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    The Enqueue function takes a final boolean parameter for this, true if you want it to be in the footer:
    http://codex.ww.wp.xz.cn/Function_Reference/wp_enqueue_script

    Thread Starter CelticPride21

    (@celticpride21)

    Would you be able to write a small example for this? I’m new to jQuery/WP integration.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    What is the code you’re using to enqueue the script at the moment?

    Thread Starter CelticPride21

    (@celticpride21)

    function my_init() {
    if (!is_admin()) {
    wp_deregister_script(‘jquery’);
    wp_register_script(‘jquery’, ‘http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js’, false, ‘1.10.2’, true);
    wp_enqueue_script(‘jquery’);

    // My Custom Script
    wp_enqueue_script(‘scripts’, get_bloginfo(‘template_url’) . ‘/javascripts/scripts.js’, array(‘jquery’), ‘1.0’, true);
    }
    }
    add_action(‘init’, ‘my_init’);

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Oh, you shouldn’t be deregistering jQuery. It can cause all sorts of problems in the future with the compatibility of your theme with the core WordPress application.

    Regarding your enqueue function you are already putting the “scripts.js” file at the bottom of your page just before the “</body>” tag by setting the last bit (that I’ve highlighted in bold) as “true”:

    wp_enqueue_script(‘scripts’, get_bloginfo(‘template_url’) . ‘/javascripts/scripts.js’, array(‘jquery’), ‘1.0’, true);

    Thread Starter CelticPride21

    (@celticpride21)

    1) Should I remove the 3rd line, ‘wp_deregister_script(‘jquery’);’

    2) So in a sense by setting wp_enqueue_script I’m appending the code to the bottom of my page(s)? If so that makes so much more sense to me and thank you so much for clearing that up!

    3) My last question would be for a lot of these plugins they have their initializer and then a completely separate .js file. Should I just include the initializer in that .js file or is there something I’m still not understanding?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    1) Replace all of your function with this:

    function my_init() {
        // My Custom Script
        wp_enqueue_script( 'scripts', get_bloginfo( 'template_url' ) . '/javascripts/scripts.js', array( 'jquery' ), '1.0', true );
    }
    add_action( 'wp_enqueue_scripts', 'my_init' );

    Thread Starter CelticPride21

    (@celticpride21)

    Worked out perfectly! Thank you. Any suggestions for #3?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    You can just put that inside your scripts.js file.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Initializing jQuery Scripts’ is closed to new replies.