Skip to content

Instantly share code, notes, and snippets.

@vishalbasnet23
Last active December 2, 2021 17:39
Show Gist options
  • Save vishalbasnet23/c9fd301e0dbc82b4f46901f0e4bb5d64 to your computer and use it in GitHub Desktop.
Save vishalbasnet23/c9fd301e0dbc82b4f46901f0e4bb5d64 to your computer and use it in GitHub Desktop.
Append async to wp_enqueue_script
<?php
function advanced_asyc_scripts($url) {
if ( strpos( $url, '#asyncload') === false ) {
return $url;
} else if ( is_admin() ) {
return str_replace( '#asyncload', '', $url );
} else {
return str_replace( '#asyncload', '', $url )."' async='async' defer='defer";
}
}
add_filter( 'clean_url', 'advanced_asyc_scripts', 11, 1 );
add_action( 'wp_enqueue_scripts', 'the_outdoor_trip_enqueue_scripts' );
function the_outdoor_trip_enqueue_scripts() {
wp_enqueue_script( 'google-map', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyA2I2cP6UiavrQHrhZBk_jVKhFsr1qYln0#asyncload', array('jquery'), '3', true );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment