• Hello WordPress folks. I’m working on a plugin for managing staff. I’ve made a custom post type and am able to display everything from the custom post, and my singe-templates work well with non-block themes.

    I’m struggling with getting my template to work with the twenty-twenty-three block theme. get_header(); and get_footer(); are defaulting to theme-compat templates. I’ve tried get_template_part( ‘header’, ‘twentytwentythree’ ); and get_template_part( ‘footer’, ‘twentytwentythree’ ); as well as get_template();.

    I’m looking for my plugin to work out of the box, at least with the default WordPress block themes as well as non-block themes and I can’t find any documentation for guidance. The idea is that a user should just be able to activate the plugin and start creating staff. Site visitors will see dedicated staff pages that inherit the same wrapper structure (header, footer, etc.) as the rest of the site. I would very much appreciate any help.

    • This topic was modified 1 year, 8 months ago by backerman78.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi there,

    WordPress block themes are built with full site editing in mind, and a lot of the traditional WordPress functions don’t work the same way as with classic themes. For instance, functions like get_header(), get_footer(), and get_template_part() are not applicable in block-based themes because these themes use blocks to render their entire structure instead of PHP templates.

    In block themes, everything (including the header and the footer) is built using blocks via HTML and a small amount of PHP. This is all controlled by theme.json and block templates. So, you won’t be able to use traditional PHP template files to override the header, footer, etc.

    Instead of custom post types and templates, you’ll need to utilize block patterns and/or block templates with your plugin. Here’s how you could potentially make your plugin compatible with both classic and block themes:

    1. Create block patterns or block templates: These can be used to add pre-designed sections to a page. You can create patterns or templates for various staff layouts and include these in your plugin.
    2. Add these patterns/templates to the block pattern directory: This will make them available to users within the block editor. They can then add these patterns/templates to their pages as needed.
    3. For classic themes, continue using custom post types and templates: You can use conditional logic to check if the active theme is a block theme or a classic theme, and then output the content accordingly.

    You can check if the active theme is a block theme by using the wp_is_block_theme() function. Here’s an example:

    if ( wp_is_block_theme() ) {
        // Code for block themes goes here.
    } else {
        // Code for classic themes goes here.
    }

    You can get started here.

    Best regards,

    Christopher Amirian

    Thread Starter backerman78

    (@backerman78)

    @christopheramirian thank you for that information. I’ve read through that and I’m still lost at a high level as to how things come together for plugins in block themes.

    I get the theme side of it for full site editing, e.g., setting up the html-esqe template parts made up of blocks for the header or footer, and then full templates that pull in the parts for the main content pages as specified in theme.json.

    Aren’t blocks basically self-contained widgets with some UI for entering micro-bits of information and some template for displaying that information? Essentially though, content items are still a specific type of content (page, post, 404, etc.).

    But I’m missing how a plugin can integrate with a block theme without there being some kind of configuration (theme.json) or template added to the theme, or being user configured vs being contained within the plugin.

    I’m also missing the possible user story with blocks. For example, the user goes in, creates a page and pulls in a staff block and fills it out to make a staff member page. The staff is a page instead of a staff right? Could I query staff for an archive? Could I manage a list of staff separate from the overall content list? Could I set staff specific taxonomies, parent pages, options, etc. all without needing to touch the theme?

    Hi there,

    You’re right; blocks are self-contained units with their own templates and configurations. However, a plugin can still integrate seamlessly with a block theme by providing block patterns, block templates, and even custom blocks. Here’s how the user story might unfold and answer your questions:

    1. Custom Post Type: You can still create a custom post type for staff within your plugin. This custom post type can have its taxonomies, meta fields, and be queried separately from posts and pages.
    2. Custom Blocks: Within your plugin, you can create custom blocks that specifically handle staff data. For example, a block that displays a staff member’s profile, including their name, image, bio, etc.
    3. Block Templates: You can create block templates within your plugin for the staff custom post type. These templates are pre-designed layouts made up of blocks, and they can automatically apply when a user creates a new staff entry. This means that users don’t have to manually create pages or add blocks for staff; the block template can handle the layout.
    4. Adding Staff: When a user wants to add a new staff member, they would create a new entry in the staff custom post type. The block template you created for staff would automatically apply, and the user could fill in the necessary information using the custom blocks you’ve created for staff data.
    5. Query Staff & Archive: Since staff is its custom post type, you can create custom queries and archives specifically for staff. You can also create a custom block that queries and displays a list of staff members, which can be added to any page by the user.
    6. Staff-Specific Options: With custom post types and custom blocks, you can create specific options, taxonomies, and parent pages just for staff, without affecting other content types or requiring changes to the theme.

    In this way, your plugin can offer a streamlined user experience for managing staff data, whether the user is using a classic theme or a block theme. The key is to create custom post types, custom blocks, and block templates within your plugin.

    For further reading and examples, you can refer to the WordPress Block Editor Handbook.

    Also this doc for the block templates that you can add in your plugin.

    Best regards.

    Christopher Amirian

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Post Single Template with Block Themes’ is closed to new replies.