Skip to content

Instantly share code, notes, and snippets.

@obiPlabon
Created May 15, 2017 11:20
Show Gist options
  • Save obiPlabon/b8fa708d2041010a9f2aa51985ae3642 to your computer and use it in GitHub Desktop.
Save obiPlabon/b8fa708d2041010a9f2aa51985ae3642 to your computer and use it in GitHub Desktop.
Modify script tag in WordPress to load scripts in async mood or defer mood
<?php
/**
* Add async boolean attribute to script tag
* @param string $tag Default script tag
* @return string Modified script tag
*/
function omm_add_async_loading( $tag ) {
return str_replace( '<script', '<script async', $tag );
}
add_filter( 'script_loader_tag', 'omm_add_async_loading' );
<?php
/**
* Add defer boolean attribute to script tag
* @param string $tag Default script tag
* @return string Modified script tag
*/
function omm_add_defer_loading( $tag ) {
return str_replace( '<script', '<script defer', $tag );
}
add_filter( 'script_loader_tag', 'omm_add_defer_loading' );
@talymo
Copy link

talymo commented Mar 22, 2018

Worked perfectly. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment