javascript – Add type to enqueued script inside plugin
So I don’t know if this is possible, but I have a question – We are integrating OneTrust and one of the requirements was to change <script type="text/javascript">
to <script type="text/plain
>.
Inside one of the plugins that we have installed on WordPress, they have their file enqueued:
wp_enqueue_script(
"juicerembed',
'//assets.juicer.io/embed-no-jquery.js',
array('jquery'),
false,
false
);
When the script is loaded, it loads it with no type which defaults to <script src="https://assets.juicer.io/embed-no-jquery.js?ver=5.8.1" id='juicerembed-js'></script>
.
Is there a way to hook or filter into a plugins enqueued scripts and add a type? Adding it inside the plugin directly will just get overwritten on next updates.
- Can you hook/filter possible?
- Do you deregister the script and then reregister?
All help will be appreciated!
Answer ( 1 )
There’s a filter:
script_loader_tag
.add_filter( 'script_loader_tag', 'wpse397773_change_script_tags', 10, 3 ); function wpse397773_change_script_tags( $tag, $handle, $src ) { $tag = '<script type="text/plain" src="' . $src . '" id="' . $handle . '-js"></script>'; return $tag; }
This should change all the
<script>
tags in the page (assuming they’re properly enqueued) to<script type="text/plain">
.Update: To only change the
<script>
tag for thejuicerembed
script, you can do this:add_filter( 'script_loader_tag', 'wpse397773_change_script_tags', 10, 3 ); function wpse397773_change_script_tags( $tag, $handle, $src ) { if ( 'juicerembed' == $handle ) { $tag = '<script type="text/plain" src="' . $src . '" id="' . $handle . '-js"></script>'; } return $tag; }