metabox - Choosing a default page tempate (Classic => Gutenberg)

This code was working in the "Classic" era to set a default page template:* Blank Page by default *add_acti

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 user73539user73539
Add a comment  | 

1 Answer 1

Reset to default 0

This 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745175805a4615185.html

相关推荐

  • metabox - Choosing a default page tempate (Classic => Gutenberg)

    This code was working in the "Classic" era to set a default page template:* Blank Page by default *add_acti

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信