Template Storage System
Block templates use a hybrid storage approach that combines theme files with database records.Theme Files (Initial Storage)
Templates initially exist as HTML files in the theme directory:Database Storage (Custom Post Types)
When users customize templates, forked versions are saved to the database: Post Type:wp_template
- Stores customized templates
- Each record represents a template
post_contentcontains block markuppost_statusindicates edit statepost_namematches template slug
wp_template_part
- Stores customized template parts
- Same structure as
wp_template - Represents semantic areas (header, footer, sidebar)
Post Meta Fields
theme
The theme identifier (slug) this template originated from.
area (template parts only)
Semantic area designation:
headerfootersidebaruncategorized
Template Synchronization
Synchronization is the process of duplicating theme templates into the database to unify template querying and resolution.The Synchronization Process
Step 1: Scan Theme Directory Read all template files from the active theme’stemplates/ and parts/ directories.
Step 2: Check Database
For each theme template file, check if a corresponding CPT record exists.
Step 3: Create Auto-Drafts
For theme templates without database records:
- Create new
wp_templateorwp_template_partpost - Set
post_statustoauto-draft - Copy content from theme file to
post_content - Set
thememeta to current theme slug
- Update
post_contentif theme file changed - Preserve records with
publishstatus (user-edited)
Post Status Meanings
auto-draft
- Template exists in theme but hasn’t been edited by user
- Content matches theme file
- Essentially a cached version of theme file
publish
- Template has been customized by user
- Content differs from theme file (or theme file doesn’t exist)
- Takes precedence over theme file during rendering
When Synchronization Occurs
During REST API RequestsBenefits of Synchronization
Unified Data Source: Queries only need to check CPT—no need to read theme files directly. Clear Edit State: Post status immediately indicates if template has been customized. Simplified Rendering: Template resolution algorithm only needs to query the database. Efficient Export: Exporting themes simply queries CPT records and writes them as files.Template Resolution Algorithm
Template resolution is how WordPress selects which template to render for the current request.Resolution Steps
1. Determine Template Hierarchy Based on the current request, determine the template hierarchy. Example for single post (ID: 42, slug: hello-world, post type: post):- Templates with
publishstatus (user-edited) take precedence - If no
publishfound, useauto-draft(theme default) - Continue down hierarchy until a match is found
- Parse template content as blocks
- Resolve any
core/template-partblocks - Render block tree to HTML
- Output to page
Template Hierarchy Examples
Homepage:Template Parts Resolution
Template parts are resolved when rendering templates that containcore/template-part blocks.
Resolution Process
1. Template Parsing Template content is parsed into blocks. 2. Detect Template Part Blocks Identifycore/template-part blocks:
Theme Switching
Template behavior when switching between themes:Switching from Theme A to Theme B
1. Theme B Activation WordPress activates Theme B. 2. Synchronization Theme B templates are synchronized to database withtheme: theme-b meta.
3. Template Resolution
Template queries now filter for theme: theme-b.
4. Theme A Templates
Theme A templates (with theme: theme-a meta) remain in database but are inactive.
Returning to Theme A
1. Theme A Reactivation WordPress activates Theme A. 2. Templates Restored Template queries now filter fortheme: theme-a, restoring previous customizations.
Template Isolation
Thetheme meta field ensures:
- Each theme’s templates are isolated
- Customizations don’t conflict between themes
- Users can switch themes without losing customizations
Exporting Block Themes
Exporting converts database templates back to theme files.Export Process
1. Query Templates- Extract
post_content - Write to appropriate directory
- Filename from
post_name
publish status templates (user customizations).
Export Considerations
Preserve Originals: Auto-draft templates can be omitted if they match theme originals. User Customizations: Publish status templates should always be included. Template Parts: Include all referenced template parts.REST API Endpoints
Templates Endpoint
List Templates:auto-draft to publish.
Delete Template:
Template Parts Endpoint
List Template Parts:Best Practices
Theme Development
Provide Comprehensive Defaults: Include all essential templates in the theme. Use Descriptive Slugs: Template slugs should be clear and follow WordPress conventions. Document Template Structure: Explain which template parts are used and where.Plugin Development
Respect Synchronization: Don’t bypass the synchronization system. Query Correctly: Always filter bytheme meta when querying templates.
Handle Both Sources:
Code should work with both theme file and database templates.