2

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

3
  • Although it's not a good idea to do this (since a badly written code in your theme might break), but have you tried the usual "disabling plugins and switching to default theme" procedure?
    – Johansson
    Jul 12, 2017 at 2:55
  • @JackJohansson no haven't tried yet, the theme is my own and I use live environment only it would be hard to do, actually one method worked, which is this stackoverflow.com/a/21167716/1235655, but I don't know how healthy is this.
    – HOY
    Jul 12, 2017 at 8:27
  • Unfortunately that's not a good practice. You are forcing every script to be loaded in the footer, which is not really recommended.
    – Johansson
    Jul 12, 2017 at 14:15

1 Answer 1

0

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');`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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