javascript – Add type to enqueued script inside plugin

Question

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.

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

All help will be appreciated!

in progress 0
theMap 1 year 2021-11-05T14:12:23-05:00 0 Answer 0 views 0

Answer ( 1 )

    0
    2021-11-08T15:52:07-05:00

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

Leave an answer

Browse
Browse