Created
October 2, 2021 03:44
-
-
Save joshhartman/ee0505a406f7aad91cae96714857fe1f to your computer and use it in GitHub Desktop.
Defer Loading CSS in WordPress to Prevent Render-Blocking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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