Skip to content

Instantly share code, notes, and snippets.

@joshhartman
Created October 2, 2021 03:44
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 joshhartman/ee0505a406f7aad91cae96714857fe1f to your computer and use it in GitHub Desktop.
Save joshhartman/ee0505a406f7aad91cae96714857fe1f to your computer and use it in GitHub Desktop.
Defer Loading CSS in WordPress to Prevent Render-Blocking
<?php
// Defer Loading CSS in WordPress to Prevent Render-Blocking
function custom_defer_css( $tag, $handle ) {
$css_to_defer = array( 'style-handle-one', 'style-handle-two' );
if ( in_array( $handle, $css_to_defer ) ) {
return str_replace( "media='all'", "media='print' onload=\"this.media='all';\"", $tag );
}
return $tag;
}
add_filter( 'style_loader_tag', 'custom_defer_css', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment