Opened 8 years ago
Last modified 5 years ago
#36791 new enhancement
Set load order when enqueuing scripts and styles
Reported by: | logistiker | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.6 |
Component: | Script Loader | Keywords: | has-patch has-unit-tests |
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)
Change History (6)
#1
@
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.
#3
@
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.
#4
@
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.
Add offset parameter when enqueuing scripts and styles