This code was working in the "Classic" era to set a default page template:
/* Blank Page by default */
add_action('add_meta_boxes', 'blank_default_page_template', 1);
function blank_default_page_template() {
global $post;
if ( 'page' == $post->post_type
&& 0 != count( get_page_templates( $post ) )
&& get_option( 'page_for_posts' ) != $post->ID // Not the page for listing posts
&& '' == $post->page_template // Only when page_template is not set
) {
$post->page_template = "page-template-blank.php";
}
}
Resulting in this behaviour:
How to achieve the same in Gutenberg? Currently, it looks like:
This code was working in the "Classic" era to set a default page template:
/* Blank Page by default */
add_action('add_meta_boxes', 'blank_default_page_template', 1);
function blank_default_page_template() {
global $post;
if ( 'page' == $post->post_type
&& 0 != count( get_page_templates( $post ) )
&& get_option( 'page_for_posts' ) != $post->ID // Not the page for listing posts
&& '' == $post->page_template // Only when page_template is not set
) {
$post->page_template = "page-template-blank.php";
}
}
Resulting in this behaviour:
How to achieve the same in Gutenberg? Currently, it looks like:
Share Improve this question asked Aug 19, 2019 at 16:19 user73539user735391 Answer
Reset to default 0This is possible with javascript using the wp.data api.
You can see this work in the console, by replacing the template name as needed:
wp.data.dispatch('core/editor').editPost( {template: "templates/blank.php"} )
You would just need to run that at some point after the editor sidebar components have loaded in a check to see if the page template has been set yet.