One way you might be able to do this is with the script_loader_tag
filter. See below:
function mind_defer_scripts( $tag, $handle, $src ) {
// array of scripts to add defer tag to
$defer = [
'https://cdn.babylonjs.com/viewer/babylon.viewer.js'
];
// check if current script should be deferred
if ( in_array( $src, $defer ) ) {
return '<script src="' . $src . '" defer="defer" type="text/javascript"></script>' . "\n";
}
return $tag;
}
add_filter( 'script_loader_tag', 'mind_defer_scripts', 10, 3 );
Note if you know the "handle" you could use that in-place of src like below:
function mind_defer_scripts( $tag, $handle, $src ) {
// array of scripts to add defer tag to
$defer = [
'babylon'
];
// check if current script should be deferred
if ( in_array( $handle, $defer ) ) {
return '<script src="' . $src . '" defer="defer" type="text/javascript"></script>' . "\n";
}
return $tag;
}
add_filter( 'script_loader_tag', 'mind_defer_scripts', 10, 3 );