Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Core's script_loader_tag filter for scripts #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion jsconcat.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,16 @@ function do_items( $handles = false, $group = false ) {
}
}
if ( isset( $href ) ) {
echo "<script type='text/javascript' src='$href'></script>\n";
$tag = "<script type='text/javascript' src='$href'></script>\n";
/**
* Filters the HTML script tag of an enqueued script.
*
* @param string $tag The `<script>` tag for the enqueued script.
* @param string $js_array The array with the type, path, and handles for the scripts being concatenated.
* @param string $href The script's source URL.
*/
$tag = apply_filters( 'script_loader_tag', $tag, $js_array, $href );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter's $handle is expected to be a string, but here it is supplied an array. If the common use-case is to modify the tag regardless of what is being concatenated then specifying a static handle would be better. Might the $js_array be passed in as an unnamed final attribute for the cases where what is being concatenated is expected to affect the tag's output?

Should we could consider a custom filter here since this is outside the expected enqueuing context of the script_loader_tag filter, even though it serves the same purpose?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only worry is that then we won't take advantage of all the themes/plugins that use this filter for this purpose?

I went this route as Atomic on WPCOM runs the same thing: https://github.com/Automattic/page-optimize/blob/master/concat-js.php#L263

echo $tag;
}
if ( isset( $js_array['extras']['after'] ) ) {
foreach ( $js_array['extras']['after'] as $inline_after ) {
Expand Down