0

We moved jQuery to the footer using the following code:

function jquery_enque_fix() {
if( !is_admin() ) {

    // Enqueue so we can grab the built-in version
    wp_enqueue_script( 'jquery' );

    // Get jquery handle - WP 3.6 or newer changed the jQuery handle (once we're on 3.6+ we can remove this logic)
    $jquery_handle = (version_compare($wp_version, '3.6-alpha1', '>=') ) ? 'jquery-core' : 'jquery';

    // Get the WP built-in version
    $wp_jquery_ver = $GLOBALS['wp_scripts']->registered[$jquery_handle]->ver;

    // Just in case it doesn't work, add a fallback version
    $jquery_ver = ( $wp_jquery_ver == '' ) ? '1.8.3' : $wp_jquery_ver;

    // De-register built-in jquery
    wp_deregister_script('jquery');

    // Register CDN version
    wp_register_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/'. $jquery_ver .'/jquery.min.js' );

    // Enqueue new jquery
    wp_enqueue_script( 'jquery' );
}
}
add_action( 'wp_enqueue_scripts', 'jquery_enque_fix');

This quickly became a problem for a lot of Wordpress plugins, so now i'm trying to move it back in the header, but nothing seems to work.

Removing the above code and replacing it with:

wp_enqueue_script('jquery');

jQuery still loads in the footer section. I've tried using the code from @dipali from this thread, which should load the code in the header. Copy/Paste and still the code went into the footer..

Any ideas on what to try next?

  • On a general note, you shouldn't deregister jquery and add your own in, use the one that comes with WP Core else you're going to get compatibility issues with Core and other plugins as they update and expect the version bundled with core – Tom J Nowell Dec 10 '14 at 12:55
  • The code actually identifies the bundled version and includes this exact one from googles library. – Jeppe Dec 11 '14 at 11:51
  • I saw =) it was just a general note – Tom J Nowell Dec 11 '14 at 12:09
0

WordPress's wp_register_script has an $in_footer parameter which allows you to state if the script should be written to wp_head or wp_footer.

By default the value is false (don't show in footer), but you could try it yourself.

Replace your wp_register_script with this:

wp_register_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/'. $jquery_ver .'/jquery.min.js', '', '', false );
  • I've tried chaning this parameter, but since the function defaults to false anyway, it provides the same result. – Jeppe Dec 11 '14 at 11:54

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.