Skip to content

Instantly share code, notes, and snippets.

@NickDuncanZA
Last active March 11, 2018 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickDuncanZA/a0effc600b8c7c0a908388688a68e4f9 to your computer and use it in GitHub Desktop.
Save NickDuncanZA/a0effc600b8c7c0a908388688a68e4f9 to your computer and use it in GitHub Desktop.
Defer certain WordPress scripts
/**
* Add 'defer' to certain scripts, dependant on their handle
*/
function fdFS2d_theme_make_script_defer( $tag, $handle, $src ) {
$script_handles = array(
'wplc-server-script',
'wplc-user-script',
'wplc-md5',
'wplc-node-server-script',
'wplc-user-js-emoji-concat',
'wplc-user-events-script',
'wplc-theme-modern',
'wplc-user-pro-editor',
'wplc-user-pro-events-script',
'wplc-user-pro-features',
'wplc-user-jquery-cookie',
'bleeper-action-script',
);
if ( in_array( $handle, $script_handles ) ) {
return str_replace( '<script', '<script defer', $tag );
}
// Just return the default tag if we didnt get a match above
return $tag;
}
add_filter( 'script_loader_tag', 'fdFS2d_theme_make_script_defer', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment