Use has_block() function to enqueue block assets
-
Thank you for an absolutely wonderful plugin! It is light weight, straight to the point, and makes the Gallery block a perfect solution without additional plugins. It would be great to see this in core.
I do see block assets are loaded on all pages. In testing with the base plugin I was able to load the JS and CSS file only when the block was loaded on the page with the following:
function ggbl_lightbox_scripts() {
if (has_block('core/gallery')) {
wp_enqueue_style( 'ggbl_lightbox_css', plugin_dir_url( __FILE__ ) . 'ggbl-lightbox.css', array(), 1 );
wp_enqueue_script( 'ggbl_lightbox_js', plugin_dir_url( __FILE__ ) . 'ggbl-lightbox.js', array( 'jquery' ), 1, true );
}
}
add_action( 'wp_enqueue_scripts', 'ggbl_lightbox_scripts' );Any chance something similar can be added in the future?
Thanks again for the great work!
The topic ‘Use has_block() function to enqueue block assets’ is closed to new replies.