Opened 3 years ago
Last modified 22 months ago
#14853 accepted enhancement
WP_Scripts::do_item filter
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | General | Version: | 3.1 |
| Severity: | normal | Keywords: | has-patch |
| Cc: | mattwiebe, jczorkmid |
Description
There should be actions that fire after a script is printed. This would enabled this best-practices usage scenario (from http://github.com/paulirish/html5-boilerplate/blob/master/index.html ):
<!-- Grab Google CDN's jQuery. fall back to local if necessary -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src="js/jquery-1.4.2.min.js"><\/script>')</script>
The before action isn't covered by that use-case, but if we're going to have an after action, we might as well have a before one.
I've attached a patch that would allow for this. Dead simple.
Attachments (2)
Change History (11)
- Keywords has-patch added
I thought about this some more, and a filter would be more appropriate than before/after actions. In addition to the benefits outlined above, this would allow a plugin to strip the type='text/javascript' when using HTML5 syntax.
- Summary changed from WP_Scripts::do_item before/after actions to WP_Scripts::do_item filter
This would also be useful for caching/minifying plugins. I've looked at WP Minify and the way it goes about its business is a bit of a nightmare. This would simplify things greatly.
- Keywords 3.2-early added
- Owner set to westi
- Status changed from new to accepted
I'll look at this when I go through and improve the Script Loader L10n support
- Cc jczorkmid added
This would also be useful for doing the jQuery.noConflict() call when using an external jQuery.
Replying to jczorkmid:
This would also be useful for doing the jQuery.noConflict() call when using an external jQuery.
I'm hoping to move that call out of our bundled jQuery and into a second script on of these days :-)
- Keywords 3.2-early removed
A filter on the entire output would certainly allow the most flexibility, including the ability to change the attributes of <script> tags.
Note however that 'wp_print_scripts' is already used as an action. 'wp_print_script' would be more appropriate.
Another idea would be to introduce an add_inline_script() function, similar to the newly introduced add_inline_style() in [18464]. Example usage:
add_inline_script( 'jquery', '!window.jQuery && document.write(\'<script src="js/jquery-1.4.2.min.js"></script>\')' );
It could also accept a third parameter, $position, which could be 'before' or 'after'.

Another example use-case that could make use of this from Typekit:
<script src="http://use.typekit.com/typekit-id.js"></script> <!-- make use of wp_print_scripts_after-typekit --> <script>try{Typekit.load();}catch(e){}</script>And I'm sure tons more. It'd be nice for plugins to make use of WP methods rather than just spitting out their code in the wp_head or wp_footer actions.