/**
* Adds 'async' or 'defer' attributes to certain script elements.
*
* @param string $tag The base HTML markup
* @param string $handle The script's handle (first parameter in wp_enqueue_script)
* @param string $src The script's source (second parameter in wp_enqueue_script)
*
* @return string The complete HTML markup for the script tag.
*/
function pixel_designs_script_special_attributes ( $tag, $handle, $src ) {
// For scripts that require the 'async' attribute, include their handles in this array.
$async = [
'google-maps-js',
];
// For scripts that require the 'defer' attribute, include their handles in this array.
$defer = [
'google-maps-js',
];
if ( in_array( $handle, $async ) ) {
$tag = str_replace( 'src', 'async src', $tag );
}
if ( in_array( $handle, $defer ) ) {
$tag = str_replace( 'src', 'defer src', $tag );
}
return $tag;
}
add_filter( 'script_loader_tag', 'pixel_designs_script_special_attributes', 10, 3 );