Make WordPress Core

Opened 8 years ago

Last modified 5 years ago

#36791 new enhancement

Set load order when enqueuing scripts and styles

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


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 8 years ago.
Add offset parameter when enqueuing scripts and styles

Download all attachments as: .zip

Change History (6)

8 years ago

Add offset parameter when enqueuing scripts and styles

#1 @swissspidy
8 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
8 years ago

  • Keywords has-patch has-unit-tests added

#3 @logistiker
8 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 8 years ago by logistiker (previous) (diff)

#4 @logistiker
8 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 8 years ago by logistiker (previous) (diff)

#5 @logistiker
8 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 8 years ago by logistiker (previous) (diff)
Note: See TracTickets for help on using tickets.