Skip to content

Instantly share code, notes, and snippets.

@ivo-ivanov
Last active April 8, 2021 06:09
Show Gist options
  • Save ivo-ivanov/932809bdfb627f83620b8b9d0e13452b to your computer and use it in GitHub Desktop.
Save ivo-ivanov/932809bdfb627f83620b8b9d0e13452b to your computer and use it in GitHub Desktop.
Load script only if the block is on the page. #wordpress #gutenberg
function enqueue_if_block_is_present(){
if(is_singular()){
//We only want the script if it's a singular page
$id = get_the_ID();
$theme_version = wp_get_theme()->get( 'Version' );
//load only if the block is on the page
if(has_block('acf/slider',$id)){
wp_enqueue_script( 'swiper', get_template_directory_uri() . '/assets/js/swiper-bundle.min.js',
array(), $theme_version,
false );
wp_script_add_data( 'swiper', 'async', true );
}
}
}
add_action('wp_enqueue_scripts','enqueue_if_block_is_present');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment