Change script type and src of plugins in theme

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

Topic wp-register-script wp-enqueue-script Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.