Skip to content

Instantly share code, notes, and snippets.

@wpflames
Created October 2, 2022 08:20
Show Gist options
  • Select an option

  • Save wpflames/eb06e01f8143ba238aa3458b1acb5a32 to your computer and use it in GitHub Desktop.

Select an option

Save wpflames/eb06e01f8143ba238aa3458b1acb5a32 to your computer and use it in GitHub Desktop.
Enqueue Scripts Asynchronously
<?php defined( 'ABSPATH' ) || exit;
// =========================================================================
// ASYNC LOAD
// =========================================================================
function my_async_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='true";
}
add_filter( 'clean_url', 'my_async_scripts', 11, 1 );
<?php
// =============================================================
// Enqueue Scripts Asynchronously
// =============================================================
function add_custom_scripts() {
wp_enqueue_script('javascript', THEME .('/dist/index.js#asyncload'), array('jquery'), '1.0', true);
}
add_action( 'wp_enqueue_scripts', 'add_custom_scripts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment