WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 3 months 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: has-patch
Focuses: Cc:

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

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

Download all attachments as: .zip

Change History (14)

comment:1 mattwiebe4 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 -->
<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.

comment:2 mattwiebe4 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 mattwiebe3 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 westi3 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: jczorkmid3 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 westi3 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 nacin3 years ago

  • Milestone changed from Awaiting Review to Future Release

comment:8 scribu3 years ago

Related: #16494

comment:9 scribu3 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'.

atimmer8 months ago

comment:10 atimmer8 months 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 8 months ago by atimmer (next)

comment:11 nacin3 months ago

  • Component changed from General to Script Loader
Note: See TracTickets for help on using tickets.