2

I need to make Wordpress not include jQuery in the <head> section of every page. The reason I need this - is because I am already including jQuery at the very bottom of the document.

I tried this: wp_deregister_script('jquery') but it doesn't work.

How does one remove jQuery from the <head> section?

  • How are you calling that function? Although the following may work function wpdocs_dequeue_script() { wp_dequeue_script( 'jquery' ); } add_action( 'wp_print_scripts', 'wpdocs_dequeue_script', 100 ); – czerspalace Feb 4 '15 at 2:01
  • That worked! Thank you so much! Before I just put wp_deregister_script('jquery') but that did not do anything. Can you post your solution as an answer instead of a comment please, so I can accept it. – YemSalat Feb 4 '15 at 2:08
1

the following may work

function wpdocs_dequeue_script() {
        wp_dequeue_script( 'jquery' ); 
} 
add_action( 'wp_print_scripts', 'wpdocs_dequeue_script', 100 );
4

Replacing the WordPress version of jQuery is generally a bad idea. Checkout Pippin's article Why Loading Your Own jQuery is Irresponsible for more information.

Instead of completely replacing jQuery, you could instead move the supplied version to the footer. However you need to be mindful that this will most likely break plugins if they load scripts in the website header.

function themename_print_jquery_in_footer( &$scripts ) {
    // Return if the website is being requested via the admin or theme customizer
    global $wp_customize;
    if ( is_admin() || isset( $wp_customize ) ) {
        return;
    }

    $scripts->add_data( 'jquery-core', 'group', 1 );
    $scripts->add_data( 'jquery-migrate', 'group', 1 );
}
add_action( 'wp_default_scripts', 'themename_print_jquery_in_footer' );
  • Thanks! I think I am sticking with my way because I know exactly what I am doing, but I would encourage everyone to use this solution instead. – YemSalat Feb 4 '15 at 4:35
  • Just read a bit better through the article and realised that most of the points being made are actually incorrect. – YemSalat Feb 4 '15 at 4:48
  • I think it brings up some good points to be mindful of but your experiences and situation may vary. I've definitely had my workload increased by both plugins and themes that have swapped out the WordPress version of jQuery for their own. – Dylan Feb 4 '15 at 5:12
  • The article is aimed at theme developers, rather then site admins. Plus it makes an incorrect assumption that not loading jQuery in 'noConflict' mode will somehow prevent one from using the jQuery variable, which is simply false. – YemSalat Feb 4 '15 at 5:32
-1
<?php 
function my_jquery_remove() {
    if (!is_admin()) {
       wp_deregister_script('jquery');
       wp_register_script('jquery', false);
    }
}
add_action('init', 'my_jquery_remove'); 
?>

Please try this it's working correct- removes jquery library js. Code from other answers removes all js

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.