Skip to content

Instantly share code, notes, and snippets.

@fwazeter
Last active August 11, 2021 10:12
Show Gist options
  • Save fwazeter/4f0229357d7544c36f72f7dca5d0d614 to your computer and use it in GitHub Desktop.
Save fwazeter/4f0229357d7544c36f72f7dca5d0d614 to your computer and use it in GitHub Desktop.
WordPress: Add block template to block editor

This adds a block template that displays in the block editor of WordPress so a user can add information in a preset way to a template. With the Template Editor, by default it adds under "Post Content." However, this file cannot be overwritten when a user is using a wp_template custom post type - so might be a little awkward to use now. It also has no impact on the front end rendering.

It's also not a great idea to use it for things like post-title as it will render post title twice in the editor, which is potentially confusing.

function register_block_template() {
	$page_type_object = get_post_type_object( 'post' );
	$page_type_object->template = array(
		array( 'core/group', array( 'layout' => array( 'inherit' => 'true' ) ),
			array(
				array( 'core/post-terms',          array( 'term' => 'category' ) ),
				array( 'core/post-date',           array( 'textAlign' => 'center' ) ),
				array( 'core/post-featured-image', array( 'align' => 'wide' ) ),
			)
		),
	);
}
add_action( 'init', 'register_block_template' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment