Created
September 29, 2022 00:20
-
-
Save sabrina-zeidan/50e9a888e2eceae6056be5526e21eded to your computer and use it in GitHub Desktop.
Do not load unused assets [WordPress]
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
//Do not load plugins' scripts and styles on every page - only where they are in use | |
add_filter( 'style_loader_tag', 'sz_stop_loading_unused_styles', 10, 2 ); // *doesn't works with 3 arguments | |
function sz_stop_loading_unused_styles( $tag, $handle) { | |
if (!is_user_logged_in()){// for FE only | |
if (is_singular('post')) {// for Posts only | |
//entire plugin | |
if (str_contains($tag, 'woocommerce')) $tag = ''; | |
//specific handle | |
//Gutenberg | |
if (in_array( $handle, ['wc-blocks-style', 'wp-block-editor', 'wp-editor', 'wp-block-library', 'wp-components'])) $tag = ''; | |
} | |
} | |
return $tag; | |
} | |
add_filter( 'script_loader_tag', 'sz_stop_loading_unused_scripts', 10, 3 ); | |
function sz_stop_loading_unused_scripts( $tag, $handle, $src ) { | |
if (!is_user_logged_in()){ // for FE only | |
if (is_singular('post')) {// for Posts only | |
//entire plugin | |
if (str_contains($tag, 'woocommerce')) $tag = ''; | |
//specific handle | |
//Gutenberg | |
if (in_array( $handle, [])) $tag = ''; | |
} | |
} | |
return $tag; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment