0

I am using this code by Peter Wilson to move jQuery to footer:

function pwcc_jquery_to_footer( &$wp_scripts ) {
      if ( is_admin() ) {
        return;
      }  /* 
      Move jQuery to the HTML footer 
      This should be fine but may cause problems with some plugins as it potentially breaks backward compatibility. Well coded plugins should be good.

      doing_it_wrong_and_loving_it
      */
      $wp_scripts->add_data( 'jquery',         'group', 1 ); 
      $wp_scripts->add_data( 'jquery-core',    'group', 1 ); 
      $wp_scripts->add_data( 'jquery-migrate', 'group', 1 ); 
    }
    add_action( 'wp_default_scripts', 'pwcc_jquery_to_footer' );

It works in my Wordpress 4.5.2.

There is, however, one complication. It loads below Akismet's js file.

I need it to be above Akismet's js file, as it better optimizes minification by the plugin W3 Total Cache.

Any way to move it on top of the footer?

0

You can pass a priority parameter to add_action. I don't actually know what priority Akismet has (default is 10), but you could start by trying this:

add_action( 'wp_default_scripts', 'pwcc_jquery_to_footer', 9);
  • Thanks. I tried but didn't work. I think it's because the 9 is prioritized over 10 for wp_default_scripts not prioritized over wp_enqueue_scripts. – Ramanan Jun 20 '16 at 16:46
  • I was assuming you used the same hook for Akismet. Is there a reason why you would use different hooks? – cjbj Jun 20 '16 at 18:28
  • Thanks. Tried that. But it moves jquery to the head! – Ramanan Jun 21 '16 at 11:14
  • You mean when you use wp_enqueue_script for both? In that case have you used the in_footer option? – cjbj Jun 21 '16 at 12:28
  • Thanks cjbj. I can of course deregister and use wp_enqueue_scripts to move jquery to footer but that hard codes the path and would like to avoid. The first code I posted uses something like $wp_scripts->add_data( 'jquery', 'group', 1 ) and 'wp_default_scripts' inside add_action to move jquery to footer. It doesn't actually use something like: wp_enqueue_script('jquery', .... ) – Ramanan Jun 22 '16 at 16:13

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.