Support » Developing with WordPress » How and where does WP load jQuery?

  • I have a custom theme which is built from the ground up.

    In the theme, I include jQuery 1.11.1

    I kept getting errors in my code and I discovered that from the wp_head() (down further) WordPress was loading another jQuery 1.11.0

    The problem was the bootstrap required jQuery to be loaded before it and because wp_head() was loading another jQuery below bootstrap, it was throwing an error.

    I have now moved wp_head() up the page, before bootstrap.js and this has fixed the problem

    BUT….

    This is a messy workaround – the pages is still loading jQuery twice, just in the order I need it now.

    What I want to know is:

    1. How is WordPress loading jQuery – Does WP load jQuery by default with no option to deactivate?
    2. How can I stop this from happening – I built the theme from scratch therefore, I do not call it anywhere – i assume it’s a WP thing…

    I tried using wp_deregister_script(‘jquery’); but that didn’t work because it appears that WP is loading jQuery without a handle (so the deregister can’t actually find it)

    How can I control what wp_head() loads?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Use wp_enqueue_script and wp_enqueue_style to load js and css, instead of calling them in header.php or footer.php.

    For example:

    function custom_scripts() {
        wp_enqueue_style( 'bootstrapstyle', get_template_directory_uri() . '/css/bootstrap.min.css' );
        wp_enqueue_script( 'bootstrap-script', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), true );
    }
    add_action('wp_enqueue_scripts', 'custom_scripts');

    https://developer.wordpress.org/reference/functions/wp_enqueue_script/

    ADding this part

    array(‘jquery’), true );}

    makes WP load jQuery first.

    Neither of these responses address the actual question.

    1. @ikaring – I know how to enqueue – I’m not seeking assistance on how to enqueue – as I mentioned, I have already resolved the issue, I am seeking knowledge of why!
    2. @jaycbrf- I have already loaded jQuery first, I have already solved the problem – as I mentioned, I am seeking knowledge of why and how WP loads it!

    I want to know how jQuery is even getting loaded and how to stop jQuery loading altogether

    If you enqueue your scripts correctly, jQuery will not be loaded twice.
    And you dont need to include your jQuery 1.11.1.
    Have you checked if plugins are loading jQuery?

    WordPress uses jQuery and jQuery UI in it’s core.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How and where does WP load jQuery?’ is closed to new replies.