Created
July 13, 2022 16:00
-
-
Save sabrina-zeidan/2e9b2b6c926048e2b2225abd6ca04789 to your computer and use it in GitHub Desktop.
Stop loading unused Gutenberg blocks 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
//In a better world block's author makes sure the block's assests are loaded only if block is actually in use (via enqueue_block_assets). For other cases: | |
add_filter( 'script_loader_tag', 'sz_stop_loading_unused_block_crap', 10, 3 ); | |
function sz_stop_loading_unused_block_crap( $tag, $handle, $src ) { | |
// Check if block's assets are loaded | |
if (str_contains($src, 'plugins/wp-swiper')) { //plugin directory here | |
if (is_singular()) {// works only on Singular | |
//TODO: //Will work for content only. if block is elsewhere or post it's archive use this https://wordpress.stackexchange.com/questions/392493/find-if-widget-block-is-active | |
$id = get_the_ID(); | |
if (has_block('da/wp-swiper-slide', $id) !== true) { //block name here | |
// to find out the name of the block in Browser Inpector (not source code) ctrl+f for "data-type" | |
$tag = ''; | |
} | |
} | |
} | |
return $tag; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment