Can’t move jQuery to footer

Below two methods did not help me. My jQuery is always on the head section. What can be the reason?

Method 1: (adding below code to function.php)

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
   wp_deregister_script('jquery');
   wp_enqueue_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js',array(),'1.9.0',true);

}

this one works when I change 'jquery', to some other string as first parameter in wp_enqueue_script. So why it doesn’t work with jquery handle?

Method 2: (adding below code to function.php)

function pwcc_jquery_to_footer( &$wp_scripts ) {
      if ( is_admin() ) {
        return;
      }
      $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' );

This code did not have any affect on the page, it modifies group which tells wheather the script should be in header or footer

1 Answer
1

Not the best way but removing the default WordPress actions that allow any scripts in the document’s head can help:

function md_footer_enqueue_scripts() {
    remove_action('wp_head', 'wp_print_scripts');
    remove_action('wp_head', 'wp_print_head_scripts', 9);
    remove_action('wp_head', 'wp_enqueue_scripts', 1);
}
add_action('wp_enqueue_scripts', 'md_footer_enqueue_scripts');`

Leave a Comment