Last active
April 8, 2021 06:09
-
-
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
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
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