Make WordPress Core

Opened 11 years ago

Closed 10 years ago

#27407 closed defect (bug) (worksforme)

wp_enqueue_scripts: jQuery loads in head despite of being included as dependency of another footer script

Reported by: marventus's profile Marventus Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.9
Component: Script Loader Keywords:
Focuses: Cc:

Description

I reported this problem here (#22244), but since it is not the exact same problem and the ticket in question is over a year old, I thought I should create a new ticket.
Using latest wp-trunk version, TwentyTwelve theme and no plugins installed, the following script registration procedure fails to load jQuery in the footer:

function tgi_jquery_in_footer_test() {
    wp_register_script('tgi-jquery-js', "//some-uri/name.js", array("jquery"), "0.1", true);
    wp_enqueue_script('tgi-jquery-js');
}
add_action('wp_enqueue_scripts', 'tgi_jquery_in_footer_test');

Even re-registering jquery with the same options and in_footer param set to true doesn't seem to cut it:

function tgi_jquery_in_footer_test() {
    wp_deregister_script("jquery");
    wp_register_script("jquery", false, array("jquery-core", "jquery-migrate"), "1.11", true);
    wp_register_script('tgi-jquery-js', "//some/uri/name.js", array("jquery"), "0.1", true);
    wp_enqueue_script('tgi-jquery-js');
}
add_action('wp_enqueue_scripts', 'tgi_jquery_in_footer_test');

Could anyone try to replicate this?

Change History (2)

#1 @bcworkz
10 years ago

::BC waves at Marv
I get the same thing on r27646 with twentyfourteen. It seems that the load in footer parm only applies to the script being registered and can never apply to jquery files. Curiously, taking the re-registration idea one more step results in jQuery failing to be referenced at all!

function tgi_jquery_in_footer_test() {
    wp_deregister_script("jquery-core");
    wp_deregister_script("jquery-migrate");
    wp_deregister_script("jquery");
    wp_register_script("jquery-core", '/wp-includes/js/jquery/jquery.js', array(), "1.11.0", true);
    wp_register_script("jquery-migrate", '/wp-includes/js/jquery/jquery-migrate.min.js', array(), "1.2.1", true);
    wp_register_script("jquery", false, array("jquery-core", "jquery-migrate"), "1.11.0", true);
    wp_register_script('tgi-jquery-js', "//some/uri/name.js", array("jquery"), "0.1", true);
    wp_enqueue_script('tgi-jquery-js');
}
add_action('wp_enqueue_scripts', 'tgi_jquery_in_footer_test');

Registering the core and migrate files without the footer parm results in proper loading, albeit in the head of course. With the footer parm included, the jQuery files are properly placed in the global $wp_scripts array to facilitate footer loading, however, the references are never output for some unknown reason. If this can be resolved, it appears footer loading of jQuery would be possible via reregistration.

This code unsurprisingly loads both scripts in the footer:

function tgi_jquery_in_footer_test2() {
    wp_register_script('tgi-jquery2-js', "//some/uri/name2.js", array("jquery"), "0.1", true);
    wp_register_script('tgi-jquery-js', "//some/uri/name.js", array("tgi-jquery2-js"), "0.1", true);
    wp_enqueue_script('tgi-jquery-js');
}
add_action('wp_enqueue_scripts', 'tgi_jquery_in_footer_test2');

But removing the footer parm from 'tgi-jquery2-js' causes it to load in head again. (Fortunately, removing the footer parm from 'tgi-jquery-js' but not 'tgi-jquery2-js' does correctly cause 'tgi-jquery2-js' to be loaded in head despite its footer parm.) This demonstrates that the footer parm is indeed only applied to the registered script and not dependencies.

#2 @jeremyfelt
10 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

With the following code, I am able to load jQuery in the footer using both Twenty Twelve and Twenty Fourteen.

function enqueue_my_scripts() {
	wp_deregister_script( 'jquery' );
	wp_register_script( 'jquery', '/wp-includes/js/jquery/jquery.js', array(), '1.11.0', true );
	wp_enqueue_script( 'my-script', '/wp-content/plugins/my-script-plugin/script.js', array( 'jquery' ), false, true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_my_scripts' );

Core's registration of jQuery must explicitly be unregistered. The new registration of jQuery must explicitly tell it to load in the footer.

I'm going to close this ticket out. Please feel free to reopen if a different result is found or expected.

Note: See TracTickets for help on using tickets.