When does WordPress automatically enqueue jQuery?

I understood that WordPress ALWAYS enqueues jQuery on all themes, thus it’s reduntant to enqueue another jQuery with the same version.

However, while developing a theme, for testing purposes I moved it to a subdomain (same domain, another WP installation, no content but the “Hello World” post).

Read More

While the first enqueued normally the jQuery, the second stopped some functionalities and looking inside I’ve found that WordPress was not including jQuery automatically anymore.

The question is: Does WordPress wait for something (don’t know, maybe a minimum of widgets on widget areas, menus, etc) to call the script? To make sure, do I have to enqueue the native jQuery manually? I’m developing a theme with the purpose to sell it on Theme Forest and they only allow the native WP jQuery (no way to dequeue it and add from another resource).

I’m using WordPress 3.8

Related posts

2 comments

  1. On the frontend, WordPress enqueues jQuery only when the admin bar is visible, usually only for users who are logged in.

    If you need jQuery, enqueue it. It will not be included twice in this case.

  2. Do WordPress wait for something (don’t know, maybe a minimum of widgets on widget areas, menus, etc) to call the script?

    No. WordPress will load the script if you tell it to using the correct method.

    You would use this method for loading scripts via your child themes functions file.

    function my_scripts_method() {
    
    wp_enqueue_script('custom-script',
    
    get_bloginfo( 'stylesheet_directory' ) . '/js/custom_script.js', array( 'jquery' ));
    }
    
    add_action( 'wp_enqueue_scripts', 'my_scripts_method' )
    

    Assumes your script is located in a folder named js in your child themes root directory.

    For parent themes, replace:

    get_bloginfo( 'stylesheet_directory' )
    

    With:

    get_template_directory_uri()
    

Comments are closed.