Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sabrina-zeidan/2e9b2b6c926048e2b2225abd6ca04789 to your computer and use it in GitHub Desktop.
Save sabrina-zeidan/2e9b2b6c926048e2b2225abd6ca04789 to your computer and use it in GitHub Desktop.
Stop loading unused Gutenberg blocks assets [WordPress]
//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