- Conditionally load block front-end scripts
- https://developer.wordpress.org/block-editor/reference-guides/packages/packages-dependency-extraction-webpack-plugin/
- https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/
blockgallery_frontend_scripts() {
if ( has_block( 'blockgallery/masonry' ) ) {
wp_enqueue_script(
'blockgallery-masonry',
plugins_url( 'dist/blockgallery-masonry.js', __FILE__ ),
array( 'jquery', 'masonry', 'imagesloaded' ),
filemtime( plugin_dir_path( __FILE__ ) . 'dist/blockgallery-masonry.js' )
);
}
}
add_action( 'wp_enqueue_scripts', 'blockgallery_frontend_scripts' );