Skip to content

Instantly share code, notes, and snippets.

@mcgwier
Last active March 10, 2020 18:09
Show Gist options
  • Save mcgwier/7f0f4afd37aea81cf181d0a237415bdb to your computer and use it in GitHub Desktop.
Save mcgwier/7f0f4afd37aea81cf181d0a237415bdb to your computer and use it in GitHub Desktop.
Instant WordPress Optimizations
// --- Front-end Enhancements and Optimizations ---- //
if (!is_admin()) {
// Function to defer or asynchronously load scripts
function js_async_attr($tag){
# Do not add defer or async attribute to these scripts
$scripts_to_exclude = array('script1.js', 'https://domain.com/wp-includes/js/jquery/jquery.js');
foreach($scripts_to_exclude as $exclude_script){
if(true == strpos($tag, $exclude_script ) )
return $tag;
}
# Defer or async all remaining scripts not excluded above
return str_replace( ' src', ' defer="defer" src', $tag );
}
add_filter( 'script_loader_tag', 'js_async_attr', 10 );
// Remove Query Strings From Static Resources
function remove_cssjs_ver( $src ) {
if( strpos( $src, '?ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
// Remove Emoji Scripts & Comment Reply JS
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
function clean_header(){ wp_deregister_script( 'comment-reply' ); } add_action('init','clean_header');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment