WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 months ago

#36791 reopened enhancement

Set load order when enqueuing scripts and styles

Reported by: logistiker Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.6
Component: Script Loader Keywords: has-patch, has-unit-tests, bulk-reopened
Focuses: Cc:

Description

I've implemented a patch and associated unit tests to add the ability to set the offset within the style or script queue when enqueuing styles or scripts. This will allow a developer to prioritize or set a strict order in which the stylesheets and/or scripts should load. This new functionality is optional and so all current code will continue to function as it always has.

Attachments (1)

add_enqueue_offset_ticket_36791.patch (9.1 KB) - added by logistiker 3 years ago.
Add offset parameter when enqueuing scripts and styles

Download all attachments as: .zip

Change History (8)

@logistiker
3 years ago

Add offset parameter when enqueuing scripts and styles

#1 @swissspidy
3 years ago

  • Component changed from General to Script Loader

Hey there,

Welcome to trac and thanks for this patch!

Can you tell us a bit about why you implemented this or why you see a need for this?

There's already way to set the load order, namely the $priority parameter of add_action() and of course the order in which you enqueue the scripts in your callback.

Adding an $offset parameter to wp_enqueue_script/wp_enqueue_style on top of that sounds rather confusing.

#2 @swissspidy
3 years ago

  • Keywords has-patch has-unit-tests added

#3 @logistiker
3 years ago

add_action is for function calls and in this case the only call is to print scripts and has no bearing on the queue order of scripts or styles. The point of adding an offset is so you don't have to worry about the order in which the style or script is enqueued and it allows you to override code that is not in your control like core functionality or plugins without modifying them. Although admittedly, if core or plugins started using this functionality, I would still not have a way to override the order. The point is that I would like certain scripts or styles to load before other ones regardless of what order they were queued in because I don't necessarily have control over that order.

Last edited 3 years ago by logistiker (previous) (diff)

#4 @logistiker
3 years ago

In addition, another reason for controlling the script or style queue directly is to prevent the same script or style from being loaded again. For example, if I used the priority in action as you suggested, I would effectively have a different script queue from the main one (via wp_enqueue_script). If the main one loaded jquery and my action that contained a script that also needed to load jquery and loaded it, I would effectively load jquery twice.

Last edited 3 years ago by logistiker (previous) (diff)

#5 @logistiker
3 years ago

The only caveat is the code would still allow core or plugins to override any offset I might want to set. In that case, I'd probably need to override the WP_Scripts or WP_Styles class so it probably wouldn't be a big issue.

Last edited 3 years ago by logistiker (previous) (diff)

#6 @iseulde
5 months ago

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

This ticket has not seen any activity in over *two* years, so I'm closing it as "wontfix".

The ticket may lack decisiveness, may have become irrelevant, or may not have gathered enough interest.

If you think this ticket does deserve some attention again, feel free to reopen.

For bugs, it would be great if you could provide updated steps to reproduce against the latest version of WordPress (5.0.2 at the time of writing). Remember images or a video can be superior to explain a problem. At the very least, quickly test again to make sure the bug still exists.

If it’s an enhancement or feature, some extra motivation may help.

Thank you for your contributions to WordPress! <3

#7 @JeffPaul
3 months ago

  • Keywords bulk-reopened added
  • Milestone set to Awaiting Review
  • Resolution wontfix deleted
  • Status changed from closed to reopened

A decision was made to reopen tickets that were closed in the bulk edit that this ticket was affected by. This ticket is being placed back into the Awaiting Review milestone so it can be individually evaluated and verified to determine if it is still relevant/valid or reproducible.

Note: See TracTickets for help on using tickets.