1

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(
        'google-recaptcha',
        'wp-polyfill',
    ),
    'version' => WPCF7_VERSION,
    'in_footer' => true,
) );

wp_register_script(
    'wpcf7-recaptcha',
    $assets['src'],
    $assets['dependencies'],
    $assets['version'],
    $assets['in_footer']
);

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);
        default:
            return $tag;
    }
}
add_filter('script_loader_tag', ns('onetrust_script_manipulation'), 10, 2);
1

0

Your Answer

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