[wp-trac] [WordPress Trac] #40276: enhancement: add a $type parameter to wp_add_inline_script()
WordPress Trac
noreply at wordpress.org
Wed May 27 19:33:27 UTC 2026
#40276: enhancement: add a $type parameter to wp_add_inline_script()
---------------------------+------------------------------
Reporter: paulschreiber | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Script Loader | Version: 4.7.3
Severity: normal | Resolution:
Keywords: 2nd-opinion | Focuses:
---------------------------+------------------------------
Comment (by westonruter):
As I [https://core.trac.wordpress.org/ticket/51124#comment:20 just
commented] on a duplicate ticket:
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.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/40276#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list