Support

Account

Home Forums Gutenberg Gutenberg blocks not loading in child theme

Solved

Gutenberg blocks not loading in child theme

  • Hello!

    I have a parent theme with the following code:
    functions.php

    /**
    * -------------------------------------------------------------------------
    * Register ACF blocks
    * We use register_block_type_from_metadata() instead of
    * register_block_type/block.json approach because we need to translate
    * title and description
    * -------------------------------------------------------------------------
    */
    add_action( 'init', 'register_acf_blocks', 5 );
    function register_acf_blocks() {
    global $path;

    foreach ( glob( get_stylesheet_directory() . '/includes/blocks/*' ) as $path ) {
    $path_end = substr(strrchr($path, "/"), 1);

    /* If file or folder contains a . do not continue. We might have
    other files in our blocks folder */
    if ( strpos($path_end, '.') === false ) {
    include $path . '/setup.inc.php';
    }
    }

    }

    in /includes/blocks/my-block/setup.inc.php:

    <?php
    register_block_type_from_metadata( $path . '/block.json', [
    'title' => _x( 'My block', 'block title', 'domain' ),
    'description' => _x( 'My block description', 'block description', 'domain' ),
    ] );

    In my child theme I have no line of code regarding ACF. I assumed that it would work. However, no blocks are loaded. Does anybody know why and what to change to make it work?

  • Ah, of course. I have to use get_template_directory() instead of get_stylesheet_directory(). Now I can see the blocks registered but the acf fields are not loaded. Do I have to add something to make the child theme use the parent themes acf-json folder?

  • Haha some serious ACF rubber duck debugging here but I hope it might help others if admins keep the thread.

    Add a new loading point in child theme’s functions.php that points to parent theme’s acf-json folder (get_template_directory()):

    function child_theme_acf_json_load_point( $paths ) {
    	// Remove the original path (optional).
    	unset($paths[0]);
    
    	// Append the new path and return it.
    	$paths[] = get_template_directory() . '/acf-json';
    
    	return $paths;
    }
    add_filter( 'acf/settings/load_json', 'child_theme_acf_json_load_point' );
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.