• I’ve read the documentation for defining block templates and locking the layout by post type (i.e. by post, page, or custom-post), but is it possible to define the layout by post_meta data like page template or post category?

    function myplugin_register_template() {
        $post_type_object = get_post_type_object( 'post' );
        $post_type_object->template = array(
            array( 'core/paragraph', array(
                'placeholder' => 'Add Description...',
            ) ),
        );
        $post_type_object->template_lock = 'all';
    }
    add_action( 'init', 'myplugin_register_template' );
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @joshuaharbor, as far as I know, there isn’t a great way to do this. Even from a user perspective, it would be tricky – the person creating the page would need to refresh the whole page to make the template take effect, and if they entered any content, it would trigger the “The content of your post doesn’t match the template assigned to your post type.” warning.

    Would block patterns help? You could create patterns for the different layouts you want, and post authors would pick the pattern to use.

    Thread Starter joshuaharbor

    (@joshuaharbor)

    Hey @ryelle, thanks for the reply. Possibly, do block patterns allow the same template_lock behavior as a block template? The goal would be to only allow users to edit content based on that layout–but not remove/rearrange blocks.

    They don’t, no. You’d be able to add the layout you want, but then users could still and or remove other blocks.

    I looked into this a bit more, and it looks like you can filter the settings passed to the editor with block_editor_settings. So you could do something like this:

    
    function yourplugin_block_editor_settings( $settings, $post ) {
    	if ( check $post type and category here ) {
    		$settings['template'] = array(
    			array(
    				'core/paragraph',
    				array(
    					'placeholder' => 'Add Description...',
    				)
    			),
    		);
    		$settings['templateLock'] = 'all';
    	}
    	return $settings;
    }
    add_filter( 'block_editor_settings', 'yourplugin_block_editor_settings', 10, 2 );
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Gutenberg Block Template for Page Template or Category’ is closed to new replies.