Home › Forums › Gutenberg › Conditionally Enqueueing Files if Page Contains Custom Block › Reply To: Conditionally Enqueueing Files if Page Contains Custom Block
Found a solution and wanted to post it in case someone was looking for the same thing. I realized I was approaching this wrong. I later found that acf_register_block_type()
function accepts a argument called 'enqueue_assets'
So now my code looks like this, and it works like a charm.
// register logo carousel block
acf_register_block_type(array(
'name' => 'logo_carousel',
'title' => __('Logo Carousel'),
'description' => __('Logo Carousel'),
'mode' => 'edit',
'render_template' => 'blocks/wp-content/logo-carousel.php',
'enqueue_assets' => function(){
wp_enqueue_style( 'slick-slider-css', 'https://cdn.jsdelivr.net/npm/[email protected]/slick/slick.css', array(), null, 'all');
wp_enqueue_script('slick-slider-js', 'https://cdn.jsdelivr.net/npm/[email protected]/slick/slick.min.js', array( 'jquery' ), null, true );
wp_enqueue_script('slick-slider-init', get_template_directory_uri() . '/blocks/scripts/logo-carousel.js', array(), null, true );
},
'category' => 'my-custom-blocks',
'icon' => 'images-alt2',
'keywords' => array( 'logo', 'carousel', 'slider'),
));
I feel kind of dumb for not figuring it out earlier, especially since I had already been using 'enqueue_script'
. Learning experience I guess.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.