• Hi, I’m creating a plugin that adds a custom post type and I want to provide a template that can be then overwritten by the theme. I know that in the theme /template folder you can just put a ‘single-post-type.html’ and it will be loaded as template for every post of ‘post-type’, I’m trying to provide the default one with this method:

    add_filter('template_include', 'include_template_file');
    function include_template_file($template) {
            if(get_post_type() != 'tagfilters_page')
                return $template;
    
          return plugin_dir_path(__FILE__) . 'partials/tag-filters-page-single.html';
    }

    But it doesn’t correctly load the block template (for now I just copied the template from ‘single.html’ in theme twentytwentytwo), it just shows an empty page and if I inspect it the HTML comments for the block theme are still there.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Block themes do not use PHP templates like non-block themes do. You can override what PHP templates are used for non-block themes with “template_include”. Overriding block theme templates would be through a different mechanism. I’m unable to knowledgeably advise about block theme template overrides. Block themes are a new paradigm for most of us, so expert advice is scarce for the time being.

    The section on block themes in the block Editor Handbook describes how to override templates from within the admin area. I suggest evaluating what that process actually does and emulating it in your plugin.

    I found the method to override block templates here: https://florianbrinkmann.com/en/define-block-templates-5678/

    /**
     * Add group block to new pages.
     * 
     * @link https://developer.www.ads-software.com/block-editor/developers/block-api/block-templates/
     */
    function slug_post_type_template() {
    	$page_type_object = get_post_type_object( 'page' );
    	$page_type_object->template = [
    		[ 'core/group', [], [
    			[ 'core/paragraph' ],
    		] ],
    	];
    }
    add_action( 'init', 'slug_post_type_template' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Block template for custom post type’ is closed to new replies.