• Resolved mahlerthetitan

    (@mahlerthetitan)


    I want to switch to Gutenberg, but have to get past the following fundamental roadblock. I fear this question is a little general. But I have a feeling I’m not the only one who’s failing to understand this key concept, and can’t find an answer anywhere else.

    I maintain a site for an artist agency. On this site, every artist gets their own page. Each of those pages has about 30 elements to them (artist name, voice type, headshot, bio, critical acclaim, audio, video, repertoire, etc.).

    Obviously I want the pages to look the same for each artist.

    However, over the coming years, I will have to create new artist pages, AND I will probably want to create or modify content sections on every artist’s page, both new and existing.

    So, here’s the scenario I’m anticipating:

    Today, I create a block template by creating a plugin with content:

    
    function myplugin_register_template() {
    $post_type_object = get_post_type_object( 'page' );
    $post_type_object->template = array(
        array( 
            'core/heading', array(
                'placeholder' => 'Artist Name'
            )
        ),
    
        array ( 
            'core/paragraph', array(
                'placeholder' => 'Voice type'
            ) 
        ),
    
        array ( 
            'core/image', array(
                'placeholder' => 'Headshot'
            ) 
        ),
    
    );
    }
    add_action( 'init', 'myplugin_register_template' );

    I can then create a new page, which – on load – has all of these blocks ready to go. Awesome!

    But, let’s say a year later, I want to add a new block to all artists’ pages on the site. For example, ‘Image Gallery’.

    If I add some code to the plugin above, then all new artist pages will have a block for the Image Gallery. But all pages previously made using the old version of this same block template will not have the Image Gallery. Even when I edit such a page, the block for Image Gallery won’t appear.

    I guess the block template only applies as I’m first creating the page, but isn’t consulted after the page is published.

    So, in order to effectively add this Image Gallery into the mix, across all artist pages, I fear I need to:

    Update the block template code above, to affect all new pages
    Manually create a Reusable Block for Image Gallery and manually insert it into all previously-published artists pages.

    Is there a better way to keep cohesion of the block template across all pages, old and new? Or, when editing a previously published page, do I have to manually survey the block elements, and make note of which block elements are missing vs. the current block template (because those elements were added to the standard block template after page was first published), and then manually add each of those?

    Have a I missed a key component? Or misunderstood a key concept? Or is there a better way to achieve my goal?

    Thanks & apologies for the fuzzy question.

    P.S. – I understand there are some plugins for this (i.e. Box Studio). I’m hesitant to go that route, having just been burned by the recently abandoned and subsequently hacked Simple Fields plugin, upon which the site was critically leaning.

Viewing 1 replies (of 1 total)
  • Plugin Author Jorge Costa

    (@jorgefilipecosta)

    Hi @mahlerthetitan,

    You may create a reusable block that may even be an empty HTML block at the start.
    On your template you can reference that reusable block:

    function myplugin_register_template() {
    	$post_type_object = get_post_type_object( 'page' );
    	$post_type_object->template = array(
    		array(
    			'core/heading', array(
    				'placeholder' => 'Artist Name'
    			)
    		),
    
    		array(
    			'core/paragraph', array(
    				'placeholder' => 'Voice type'
    			)
    		),
    
    		array(
    			'core/image', array(
    				'placeholder' => 'Headshot'
    			),
    		),
    		array(
    			'core/block', array(
    				'ref' => 27885,// Reusable block id
    			),
    		),
    
    	);
    	}
    	add_action( 'init', 'myplugin_register_template' );

    This will include the reusable block by default in the template.
    If later you need to add something to all posts, you can change the reusable block.

    I hope you find my answer useful, feel free to comment if you need additional clarification.

Viewing 1 replies (of 1 total)
  • The topic ‘Applying block template to new and existing posts/pages’ is closed to new replies.