Make WordPress Core

Opened 5 years ago

Last modified 7 weeks ago

#14853 accepted enhancement

WP_Scripts::do_item filter

Reported by: mattwiebe Owned by: westi
Milestone: Future Release Priority: normal
Severity: normal Version: 3.1
Component: Script Loader Keywords: needs-patch
Focuses: Cc:


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 (3)

wp-scripts-filters.diff (569 bytes) - added by mattwiebe 5 years ago.
wp-scripts-filter-2.diff (551 bytes) - added by mattwiebe 5 years ago.
14853.3.diff (3.8 KB) - added by atimmer 2 years ago.

Download all attachments as: .zip

Change History (18)

comment:1 @mattwiebe5 years ago

  • Cc mattwiebe added

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 -->

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.

comment:2 @mattwiebe5 years ago

  • 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.

comment:3 @mattwiebe5 years ago

  • 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.

comment:4 @westi5 years ago

  • 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

comment:5 follow-up: @jczorkmid5 years ago

  • Cc jczorkmid added

This would also be useful for doing the jQuery.noConflict() call when using an external jQuery.

comment:6 in reply to: ↑ 5 @westi5 years ago

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 :-)

comment:7 @nacin5 years ago

  • Milestone changed from Awaiting Review to Future Release

comment:8 @scribu4 years ago

Related: #16494

comment:9 @scribu4 years ago

  • 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'.

@atimmer2 years ago

comment:10 @atimmer2 years ago

  • Cc atimmermans@… added

14853.3.diff is a patch to add wp_add_inline_script the way scribu proposed. I really like this idea because it is consistent with wp_add_inline_style and it is very clear for developers.

I haven't added the possibility for the third parameter, because I couldn't think of any use case for 'before', if you can I will add the possibility.

Version 0, edited 2 years ago by atimmer (next)

comment:11 @nacin20 months ago

  • Component changed from General to Script Loader

comment:12 @TobiasBg9 months ago

#30617 was marked as a duplicate.

comment:13 follow-up: @aristath2 months ago

Is there any reason why we haven't implemented this one yet?
If we're just waiting for a patch I could just start working on it...

comment:14 in reply to: ↑ 13 @samuelsidler2 months ago

Replying to aristath:

Is there any reason why we haven't implemented this one yet?
If we're just waiting for a patch I could just start working on it...

I don't see any reason, but one may come up with a new patch. :) Working up a patch would be a good way to see what other issues exist.

comment:15 @johnbillion7 weeks ago

  • Keywords needs-patch added; has-patch removed
Note: See TracTickets for help on using tickets.