Support

Account

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/slick-carousel@1.8.1/slick/slick.css', array(), null, 'all');
    			wp_enqueue_script('slick-slider-js', 'https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/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.