“I think that we'll always have something called themes and something like a theme directory....to users, I think it's really key that there's something called a theme that allows them to get a head start on beautiful design that, that they don't have to start from scratch and design themselves.”
Matt Mullenweg, February 2020
As this space evolves, how can we work with what is available today making our themes block enabled, while also preparing for the next generation of theme building?
Any theme that takes advantage of Gutenberg Theme Supports for Block rendering in the Editor and on the Front End.
The Block Editor provides new functionality to take advantage of Gutenberg blocks.
Editor Color Palette
Editor Text Size Palette
Responsive Embeds
Frontend & Editor Styles
Dark Mode
function sideways_setup_theme_supported_features() {
add_theme_support(
'editor-color-palette',
[
[
'name' => __( 'sidetrack purple', 'themeLangDomain' ),
'slug' => 'sidetrack-purple',
'color' => '#773B7A',
],
]
);
}
add_action( 'after_setup_theme', 'sideways_setup_theme_supported_features' );
The block editor also provides opt-in opinionated styles you can access.
add_theme_support( 'align-wide' );
Some blocks such as the image block have the possibility to define a “wide” or “full” alignment by adding the corresponding classname to the block’s wrapper ( alignwide or alignfull ).
add_theme_support( 'wp-block-styles' );
Core blocks include default styles. The styles are enqueued for editing but are not enqueued for viewing unless the theme opts-in to the core styles.
The block editor adds additional markup to floated images and images with captions to make styling them easier.
add_theme_support( 'editor-color-palette', array(
array(
'name' => __( 'sidetrack dark space', 'themeLangDomain' ),
'slug' => 'sidetrack-dark-space',
'color' => '#a156b4',
),
array(
'name' => __( 'sidetrack neon purple', 'themeLangDomain' ),
'slug' => 'sidetrack-neon-purple',
'color' => '#d0a5db',
),
) );
Different blocks have the possibility of customizing colors. The block editor provides a default palette, but a theme can overwrite it and provide its own.
name is a human-readable label that appears in the tooltip and provides a meaningful color description.
slug is a unique identifier used to generate the CSS classes for the block editor color palette.
color is the hexadecimal code to specify the color.
add_theme_support( 'disable-custom-colors' );
This flag will make sure users are only able to choose colors from the editor-color-palette the theme provided or from the editor default colors if the theme did not provide one.
add_theme_support(
'editor-gradient-presets',
array(
array(
'name' => __( 'Vivid cyan blue to vivid purple', 'themeLangDomain' ),
'gradient' => 'linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)',
'slug' => 'vivid-cyan-blue-to-vivid-purple'
),
)
);
Different blocks have the possibility of selecting from a list of predefined gradients. The block editor provides a default gradient presets, but a theme can overwrite them and provide its own
.has-vivid-cyan-blue-to-vivid-purple-gradient-background {
background: linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%);
}
Themes are responsible for creating the classes that apply the gradients. To apply the gradient, implement the corresponding class.
name is a human-readable label that appears in the tooltip and provides a meaningful gradient description.
gradient is the CSS value of a gradient applied to a background-image of the block
slug is a unique identifier for the gradient used to generate CSS classes used by the block editor
add_theme_support( 'disable-custom-gradients' );
When set, users will be restricted to the default gradients provided in the block editor or the gradients provided via the editor-gradient-presets theme support setting.
add_theme_support( 'editor-font-sizes', array(
array(
'name' => __( 'Small', 'themeLangDomain' ),
'size' => 12,
'slug' => 'small'
),
array(
'name' => __( 'Regular', 'themeLangDomain' ),
'size' => 16,
'slug' => 'regular'
) );
Blocks may allow the user to configure the font sizes they use, e.g., the paragraph block. Blocks provide a default set of font sizes, but a theme can overwrite it and provide its own.
.has-regular-font-size {
font-size: 16px;
}
Themes are responsible for creating the classes that apply the correct font size styles. The class name is built appending ‘has-‘, followed by the font size name using kebab case and ending with -font-size.
add_theme_support('disable-custom-font-sizes');
When set, users will be restricted to the default sizes provided in the block editor or the sizes provided via the editor-font-sizes theme support setting.
add_theme_support('editor-styles');
The block editor supports the theme’s editor styles, however it works a little differently than in the classic editor.
add_editor_style( 'style-editor.css' );
Use the add_editor_style function to enqueue and load CSS on the editor screen. For the classic editor, this was the only function needed to add style to the editor.
Embed blocks automatically apply styles to embedded content to reflect the aspect ratio of content that is embedded in an iFrame. A block styled with the aspect ratio responsive styles would look like.
add_theme_support( 'responsive-embeds' );
To make the content resize and keep its aspect ratio, the
element needs the wp-embed-responsive class. This is not set by default, and requires the theme to opt in to the responsive-embeds feature.Block Templates are a set of predefined blocks for content defined in the editor and stored in post_content.
Block templates allow theme builders to provide the admin user with guiderails to add content.
Block Templates impose structure on post content,
and can be used independently or within current page templates.
option 1 Use PHP to associate the Block Template
with a Post Type
OR
option 2 Use JS to create Block Template as a freestanding block that can be inserted anywhere.
function sidetrack_register_template() {
$post_type_object = get_post_type_object( 'post' );
$post_type_object->template = array(
array( 'core/media-text' ),
array( 'core/heading' ),
array( 'core/list' ),
array( 'core/paragraph' ),
);
}
add_action( 'init', 'sidetrack_register_template' );
Creates a function that adds an array of blocks for admin users to populate.
function sidetrack_register_page_template() {
$post_type_object = get_post_type_object( 'post' );
$post_type_object->template = array(
array( 'core/media-text' ),
array( 'core/heading' ),
array( 'core/list' ),
array( 'core/paragraph' ),
);
}
add_action( 'init', 'sidetrack_register_page_template' );
import { __ } from '@wordpress/i18n';
import { registerBlockType } from '@wordpress/blocks';
import {
InnerBlocks,
} from '@wordpress/block-editor';
const SIDESTREET_PROPERTY_TEMPLATE = [
[ 'core/heading', { placeholder: 'Listing Price'} ],
[ 'core/paragraph', { placeholder: 'Listing Description' } ],
[ 'core/image', {} ],
[ 'core/list', { placeholder: 'Property Features' } ],
[ 'core/gallery', {} ],
];
registerBlockType( 'sidetrack/property-template', {
title: __( 'Sidetreet Property Template', 'sidetrack' ),
icon: {
foreground: '#fff',
background: '#773B7A',
src: 'multisite',
},
category: 'sidetrack',
attributes: {
content: {
type: 'string',
},
},
edit: ( props ) => {
const { className } = props;
return (
)}
/>
);
},
save: ( props ) => {
const { className } = props;
return (
);
},
});
function sidetrack_register_property_post_type() {
$args = array(
'public' => true,
'label' => 'Property',
'template_lock' => all,
'show_in_rest' => true,
'template' => array(
array( 'core/image', array(
'align' => 'left',
) ),
array( 'core/heading
', array(
'align' => 'right',
) ),
array( 'core/heading', array(
'placeholder' => 'Price',
) ),
array( 'core/paragraph', array(
'placeholder' => 'Property Description',
) ),
),
);
register_post_type( 'property', $args );
}
add_action( 'init', 'sidetrack_register_property_post_type' );
'show_in_rest' => true,',
Ensures attribute is shown in block editor.
'placeholder' => 'Listing price',
Add placeholder text
'align' => 'left',
Block Alignment
'template_lock' => 'all',
Locks blocks so admin can't move or insert
'template_lock' => 'insert',
Locks blocks so admin can't insert new blocks but can move blocks in the template.
$post_type_object->template = array(
array( 'core/media-text' ),
array( 'core/heading' ),
array( 'core/list' ),
array( 'core/paragraph' ),
);
By creating block templates we can more clearly see the modular components of our sites.
By eliminating the options, the admin user can quickly get this listing onto the site.
Patterns are predefined block layouts for a page ready to insert.
New API introduced in Gutenberg 7.8.
Experimental and subject to change.
Not a part of core...yet.
The register_block_pattern function receives the name of the pattern as the first argument and an array describing properties of the pattern as the second argument.
The properties of the style array must include name and label:
title:A human-readable title for the pattern.
content:Raw HTML content for the pattern.
function sidetreet_register_block_pattern() {
if ( function_exists( 'register_block_pattern' ) ) {
register_block_pattern(
'sidestreet-listing-pattern',
array(
'title' => __( 'Intro paragraph with two columns', 'sidestreet-listing-pattern' ),
'content' => "\n\n\n\n\n\n\n\n\n\n\n\nh3 heading/features
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nh4 heading/gallery
\n\n\n\n\n\n\n\n\n\n",
)
);
}
}
add_action( 'init', 'sidetreet_register_block_pattern' );
Block Templates are a predefined set of blocks that can be used programatically.
Block Patterns are a combination of blocks used to produce a layout.
Block Pattern | Block Template | |
---|---|---|
Resuable | Yes | Yes |
Lockable | No | Yes |
Nestable | Yes | Sort of |
The rise of the meta box came about from the limitation of structuring content in the Classic WordPress editor.
Using meta boxes to create page layouts has direct ramifcations on your database and where things are stored.
Namely, it moves what should be post_content into meta.
Similarly, now blocks can take what we want stored as meta and place it instead into post_content.
When you have information that is meta data not post content, you can create a specific block.block attributes
attributes: {
image: {
type: 'object',
source: 'meta',
meta: 'propertyimg'
},
price: {
type: 'string',
source: 'meta',
meta: 'price'
},
},
function sidetrack_property_meta() {
register_post_meta(
'post',
'image',
array(
'type' => 'string',
'single' => true,
'show_in_rest' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
)
);
register_post_meta(
'post',
'price',
array(
'type' => 'string',
'single' => true,
'show_in_rest' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
)
);
}
add_action( 'init', 'sidetrack_property_meta' );
An evolution of the editing experience allowing all areas of the site including header, footer, navigation directly in the editor using blocks.
What will applying the block experience
across WordPress look like?
Block-Based Themes
Block-Based Theme Templates
Full Site Editing
Global Styles
A WordPress theme created with HTML files built entirely with blocks. Block-based themes are minimal html structures that provide the containers for rendering blocks in the browser.
You can test it from the Site Editor Beta under the experimental flag in the Gutenberg Plugin.
theme
|__ style.css
|__ functions.php
|__ block-templates
|__ index.html
|__ single.html
|__ archive.html
|__ ...
|__ block-template-parts
|__ header.html
|__ footer.html
|__ sidebar.html
|__ ...
Footer
Very different than how we create templates now, in that they do not have typical post_content.
Made specifically for block-based themes, they provide the dynamic content areas like post_content previously displayed from the_loop.
Post Content
Post Author
Post Comments Count
Post Comments Form
Post Excerpt
Post Featured Image
Post Tags
1 > Create new under Appearance/Template Parts
2 > Apply styling using the editor
3 > Add block style variations and theme CSS
4 > Copy and paste code from code editor
into an .html file saved in
block-templates-parts folder
Block Theme Templates share the same naming convention as current theme structures and represent another layer on top of the current template hierarchy.
This could allow theme authors to make progressive enhancements using both html and php page templates if necessary.
FSE could put a lot of design control into the hands of the user — both a positive and a negative.
Global Styles is currently in development and will allow for styling across a theme.
Nothing is set in stone. The more we test and iterate the stronger the experience will become.
Join the fun.