Forked from sabrina-zeidan/avoid_loading_unused_assets.php
Created
May 3, 2023 21:35
-
-
Save positiveque/fa7df86bc604b1e924b99f9d614e0c61 to your computer and use it in GitHub Desktop.
Do not load unused assets [WordPress]
This file contains hidden or 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