Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#35643 closed defect (bug) (fixed)

wp_enqueue_script() with an alias handle doesn't work in footer

Reported by: kovshenin's profile kovshenin Owned by:
Milestone: 4.5 Priority: normal
Severity: normal Version:
Component: Script Loader Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

When registering an alias script (similar to how jquery is an alias for jquery-core and jquery-migrate) in the footer, none of the declared dependencies are processed.

This works:

add_action( 'wp_enqueue_scripts', function() {
	wp_register_script( 'foo', false, array( 'bar', 'baz' ), '1.0' );
	wp_register_script( 'bar', home_url( 'bar.js' ), array(), '1.0' );
	wp_register_script( 'baz', home_url( 'baz.js' ), array(), '1.0' );

	wp_enqueue_script( 'foo' );
});

This doesn't (no output):

add_action( 'wp_enqueue_scripts', function() {
	wp_register_script( 'foo', false, array( 'bar', 'baz' ), '1.0', true );
	wp_register_script( 'bar', home_url( 'bar.js' ), array(), '1.0', true );
	wp_register_script( 'baz', home_url( 'baz.js' ), array(), '1.0', true );

	wp_enqueue_script( 'foo' );
});

Attachments (1)

35643.diff (3.6 KB) - added by kovshenin 10 years ago.

Download all attachments as: .zip

Change History (8)

@kovshenin
10 years ago

#1 @kovshenin
10 years ago

  • Keywords has-patch has-unit-tests added

In 35643.diff: Move the src empty check to do_item() after groups have been checked.

#2 @swissspidy
10 years ago

  • Milestone changed from Awaiting Review to 4.5

#3 @grapplerulrich
10 years ago

Have you tried the patch with jQuery? I tried the patch with the following snippet and it did not work.

<?php
function grappler_move_jquery_to_footer( &$scripts ) {
    if( is_admin() ) {
        return;
    }
    $scripts->add_data( 'jquery', 'group', 1 );
    $scripts->add_data( 'jquery-core', 'group', 1 );
    $scripts->add_data( 'jquery-migrate', 'group', 1 );
}
add_action( 'wp_default_scripts', 'grappler_move_jquery_to_footer', 11 );

When I was trying to make it work I looked at https://github.com/WordPress/WordPress/blob/4.4-branch/wp-includes/class.wp-dependencies.php#L111. It adds the alias script to the header without checking the group.

#4 @kovshenin
10 years ago

@grapplerulrich Yeah it works with jQuery. The code you referenced is the code that's moved out of the dependencies class and into the scripts and styles subclasses instead. Are you sure you applied the patch? And also, make sure no other header scripts declare a jquery dependency, otherwise WordPress will still obey the dependency and print jQuery in the header, regardless of its footer group.

#5 @grapplerulrich
10 years ago

The patch works perfectly. I did not apply the patch correctly. Forgot to remove the code in class.wp-dependencies.php

#6 @ocean90
10 years ago

In 36559:

Tests: Add a test for testing wp_enqueue_script() with an alias handle in the footer.

Props kovshenin.
See #35643.

#7 @ocean90
10 years ago

  • Resolution set to fixed
  • Status changed from new to closed
  • Version trunk deleted

This was fixed in [36550].

Note: See TracTickets for help on using tickets.