0

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='//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.

  1. Can you hook/filter possible?
  2. Do you deregister the script and then reregister?

All help will be appreciated!

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 the juicerembed 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;
}

I haven't tested this at all, but hopefully it'll be a starting point for you.

2
  • Thanks so much @Pat! Is it possible to alter just one single script tag with that filter so I don't change them all? I've got them all updated except the one above, so if something is off, I'd like the above to just be filtered.
    – theMap
    Nov 5 '21 at 20:36
  • Yeah, you can check to see if the $handle matches the script's handle. I've updated my answer with a code snippet to check for the juicerembed handle you've got in your original question.
    – Pat J
    Nov 5 '21 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.