Support

Account

Home Forums Gutenberg enqueue_block_assets Inside Block Template

Unread

enqueue_block_assets Inside Block Template

  • I’m trying to register a stylesheet for a block inside the block template. When putting the following code inside my block template it doesn’t run on the front-end of the website. The stylesheet it loading fine in the admin/editor.

    When moving this code to either the theme or even up one level to be on the same page as my “acf_register_block” it loads the stylesheet just fine on the front end.

    Is there something happening inside of “render_callback” (also tried “render_template”) that would make “enqueue_block_assets” or “wp_enqueue_style” not work properly?

    Maybe I shouldn’t or can’t put the “wp_enqueue_style” inside of the template? I just thought it would be nice to have the CSS for custom blocks right along side the block.

    		// Check function exists.
    		if ( function_exists( 'acf_register_block' ) ) {
    
    			// Register Testimonial Block.
    			acf_register_block(
    				array(
    					'name'            => 'testimonial',
    					'title'           => __( 'Testimonial' ),
    					'description'     => __( 'A custom testimonial block.' ),
    					//'render_template' => META13_DIR . 'blocks/testimonial/content-testimonial.php',
    					'render_callback'	=> 'acf_block_render_callback',
    					'category'        => 'formatting',
    					'icon'            => 'admin-comments',
    					'keywords'        => array( 'meta13', 'testimonial', 'quote' ),
    					'mode'            => 'edit', // preview or edit
    					// 'align'        => 'full', // left, center, right, wide, full.
    				)
    			);
    		}
    function acf_block_render_callback( $block ) {
    	// convert name ("acf/testimonial") into path friendly slug ("testimonial")
    	$slug = str_replace('acf/', '', $block['name']);
    
    	// include a template part from within the "/blocks" folder
    	if( "wp-content/mu-plugins/meta13-functionality/blocks/{$slug}/content-{$slug}.php") {
    		include( "wp-content/mu-plugins/meta13-functionality/blocks/{$slug}/content-{$slug}.php" );
    	}
    }
    
    /**
     * Enqueue Gutenberg block assets for both frontend + backend.
     *
     * @uses {wp-editor} for WP editor styles.
     * @since 1.0.0
     */
    function meta13_blocks_cgb_block_assets() { // phpcs:ignore
    	// Styles.
    	wp_enqueue_style(
    		'meta13_blocks-cgb-style-css', // Handle.
    		'/wp-content/mu-plugins/meta13-functionality/blocks/testimonial/style.css', // Block style CSS.
    		array( 'wp-editor' ) // Dependency to include the CSS after it.
    	);
    }
    
    // Hook: Frontend assets.
    add_action( 'enqueue_block_assets', 'meta13_blocks_cgb_block_assets' );
Viewing 1 post (of 1 total)

The topic ‘enqueue_block_assets Inside Block Template’ is closed to new replies.