WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 8 months ago

Last modified 5 months ago

#14488 closed defect (bug) (fixed)

wp_enqueue_script in_footer doesn't work on WP known scripts

Reported by: mcr2582 Owned by: sorich87
Milestone: 4.2 Priority: normal
Severity: minor Version: 3.0.1
Component: Script Loader Keywords: has-patch
Focuses: Cc:

Description

I changed some plugin code from:
wp_enqueue_script('jquery-ui-sortable');

To:
wp_enqueue_script('jquery-ui-sortable',false,array(),false,true);

However, the script was not moved to the footer.

When I looked at the source for wp_enqueue_script, I can see that it's because the check for $in_footer is inside the condition for $src being defined. But built-in scripts don't require a source.

Attachments (2)

14488.diff (616 bytes) - added by sorich87 5 years ago.
14488.patch (650 bytes) - added by SergeyBiryukov 4 years ago.

Download all attachments as: .zip

Change History (17)

comment:1 @nacin5 years ago

  • Milestone changed from Awaiting Review to 3.1

@sorich875 years ago

comment:2 @sorich875 years ago

  • Cc sorich87@… added
  • Keywords has-patch added; JS footer enqueue removed
  • Owner set to sorich87
  • Status changed from new to accepted

comment:3 @nacin5 years ago

  • Milestone changed from 3.1 to Future Release

Don't think is_footer is the right check.

comment:4 @mitchoyoshitaka4 years ago

I think is_footer is actually correct there. Would be nice to have this sorted out.

@SergeyBiryukov4 years ago

comment:5 @SergeyBiryukov4 years ago

Simplified the patch a bit.

comment:6 @MikeHansenMe3 years ago

tested and confirmed the patch works.

comment:7 @MikeHansenMe3 years ago

  • Cc mdhansen@… added

comment:8 @valeriosza23 months ago

I think closing the valid ticket.

comment:9 @nacin20 months ago

  • Component changed from General to Script Loader

comment:10 @wonderboymusic8 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 31028:

When calling wp_enqueue_script() with a registered $handle, no $src (using the default value, false), and true as the value for $in_footer, ensure that the script actually loads in the footer. If the handle was registered with $in_footer equal to true, this already worked. Make it work for scripts like where $in_footer was initially false, example: wp_enqueue_script( 'json2', false, array(), false, true );

Props SergeyBiryukov.
Fixes #14488.

comment:11 @wonderboymusic8 months ago

  • Milestone changed from Future Release to 4.2

comment:12 follow-up: @sc0ttkclark7 months ago

This introduces a change which now throws notices when $handle is an array. Of course, the wp_enqueue_script and wp_enqueue_style functions are not documented to accept arrays, they may be (and have been) used as such -- even by at least one of the most popular plugins on wordpress.org.

Up until earlier this month in the trunk commit for 4.2 [31028], the unintended consequences of calling the following code were that multiple handles could be enqueued at once. Documentation in code and in codex says it's a string, but it threw no notices or errors, or doing it wrong's.

wp_enqueue_script( array( 'jquery', 'etc', 'etc2' ) );

With that, should a new ticket be opened up to add documented support for array as $handle in wp_enqueue_script and wp_enqueue_style, or would this ticket need to have a new patch applied which at minimal removes the notice that's introduced in 4.2 for undocumented use of arrays?

comment:13 in reply to: ↑ 12 @SergeyBiryukov6 months ago

Replying to sc0ttkclark:

This introduces a change which now throws notices when $handle is an array.

Related: #31636

comment:14 @slackbot5 months ago

This ticket was mentioned in Slack in #core by drew. View the logs.

comment:15 @ocean905 months ago

In 31887:

Avoid a PHP notice in wp_enqueue_script() if $handle is an array.

Calling wp_enqueue_script() with an array as the first argument is a "hidden feature" and should be avoided. Use dependencies instead.

props sorich87 for initial patch.
fixes #31636.
see #14488.

Note: See TracTickets for help on using tickets.