[wp-trac] [WordPress Trac] #51124: Can we get an additional parameter in wp_add_inline_script to set the script type?
WordPress Trac
noreply at wordpress.org
Wed May 27 19:32:18 UTC 2026
#51124: Can we get an additional parameter in wp_add_inline_script to set the
script type?
--------------------------------------------+------------------------
Reporter: hcabrera | Owner: audrasjb
Type: feature request | Status: closed
Priority: normal | Milestone:
Component: Script Loader | Version:
Severity: normal | Resolution: duplicate
Keywords: early has-patch has-unit-tests | Focuses:
--------------------------------------------+------------------------
Changes (by westonruter):
* status: accepted => closed
* resolution: => duplicate
* milestone: Future Release =>
Comment:
Indeed the `wp_inline_script_attributes` filter can be used for this. I
don't think adding another `$type` positional parameter is necessarily
going to make the most sense. For example, it doesn't make sense for you
to add an inline `after` script which is `application/json`. Really such
supporting data scripts should be added //before// if anything.
I think it would perhaps be better to not rely on inline scripts for this
at all, or the `wp_inline_script_attributes` filter either. Instead, there
can be something like:
{{{#!php
<?php
add_action(
'wp_print_footer_scripts',
static function () {
if ( ! wp_script_is( 'foo' ) ) {
return;
}
wp_print_inline_script_tag(
wp_json_encode( get_foo_data(), JSON_HEX_TAG |
JSON_UNESCAPED_SLASHES ),
array( 'type' => 'application/json' )
);
}
);
}}}
This conditionally prints the supporting JSON script tag if the `foo`
script is enqueued.
In any case, I'm closing as a duplicate of #40276.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/51124#comment:20>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list