So I have to implement OneTrust which requires all <script type="text/javascript"> to be converted to <script type="text/plain"> and there are various plugins that use script tags that I need to change.

So let's say that Contact Form 7 has the following snippet in their plugin:

$assets = wp_parse_args( $assets, array(
    'src' => wpcf7_plugin_url( 'modules/recaptcha/index.js' ),
    'dependencies' => array(
    'version' => WPCF7_VERSION,
    'in_footer' => true,
) );


wp_enqueue_script( 'wpcf7-recaptcha' );

How do I properly change all the types and src to data-src of each enqueued script individually?

Also, how would I go about adding classes to the script tag?

Do I deregister and deenqueue each script and then register and enqueue it again?

I've tried using the following to change the script src, but it didn't work:

function onetrust_script_manipulation($tag, $handle) {
    switch($handle) {
        case 'google-recaptcha':
            return str_replace(' src', ' data-src', $tag);
            return $tag;
add_filter('script_loader_tag', ns('onetrust_script_manipulation'), 10, 2);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.