Using the block editor for theme-parts

Full Site Editing (FSE for short) is scheduled to be included in WordPress 5.6 in a few months. In case you are not aware of what FSE is, it’s an effort to bring the BlockBlock Block is the abstract term used to describe units of markup that, composed together, form the content or layout of a webpage using the WordPress editor. The idea combines concepts of what in the past may have achieved with shortcodes, custom HTML, and embed discovery into a single consistent API and user experience. Editor to all areas of a site, including the site headerHeader The header of your site is typically the first thing people will experience. The masthead or header art located across the top of your page is part of the look and feel of your website. It can influence a visitor’s opinion about your content and you/ your organization’s brand. It may also look different on different screen sizes., footer, sidebars and everything in between.

Reusable Blocks are a pretty neat and relatively underestimated feature of the Block Editor that can allow you to implement these things in your theme now, easing the transition to a full-fledged editing experience.

The idea is this: You can create a reusable block for your site’s header or footer, and inject that where your normal theme’s header or footer would be. Reusable blocks are a separate post-type called wp_block and can be queried like any other post-type, so you can do pretty much anything you want with them.

Below I’m going to share some example code snippets that you can use & modify to suit your needs. The examples will be for a header, but you can duplicate the same logic for footers, sidebars and so on.

First, we’ll need a function that will check if a reusable-block with that title exists. If it does exist we’re going to print its content, and if it doesn’t exist we’re going to return false to make things easier down the road:

/**
 * Prints the header reusable block.
 * If a "site-header" reusable-block doesn't exist, return false.
 * 
 * @return bool
 */
function my_theme_the_header_reusable_block() {

	// Run the query.
	$posts = get_posts( [
		'post_type' => 'wp_block',
		'title'     => 'site-header',
	] );

	// If a block was located print it and return true.
	if ( $posts && isset( $posts[0] ) ) {
		echo do_blocks( $posts[0]->post_content );
		return true;
	}

	// If we got this far the header block doesn't exist.
	// Return false.
	return false;
}

Next, we’re going to add a function to print the contents of that 1st function. If a block doesn’t exist, we’re going to print a fallback header by calling get_template_part( 'fallback-header' ), and then we’ll add a message urging the site-admin to create a new header. This way users won’t start wondering “why is there no header”, they can just click a link and create one:

/**
 * Print the header content.
 * 
 * If a "site-header" reusable block exists we print its contents here
 * otherwise add a link so admins can create a new header.
 */
function my_theme_the_header() {
	?>
	<header>
		<?php // Print the header. If one doesn't exist, print custom content. ?>
		<?php if ( ! my_theme_the_header_reusable_block() ) : ?>
			<?php get_template_part( 'fallback-header' ); ?>
			<?php if ( current_user_can( 'edit_theme_options' ) ) : ?>
				<p>
					<?php esc_html_e( 'It looks like you have not yet created a header for your site.', 'textdomain' ); ?>
				</p>
				<p>
					<a href="<?php echo esc_url( admin_url( 'post-new.php?post_type=wp_block&post_title=site-header' ) ); ?>">
						<?php esc_html_e( 'Create a new header', 'textdomain' ); ?>
					</a>
				</p>
			<?php endif; ?>
		<?php endif; ?>
	</header>
	<?php
}

When they click on the link generated from this function, users will be redirected to a page where they can add the contents of their header, and to make things easier the site-header title is already filled-in for them.

The above 2 functions can be included in our theme’s functions.php file, or anywhere else we want them depending on the theme’s structure.

To print this user-created header in our theme we’ll just have to go in our header.php file and call the my_theme_the_header() function.That’s all there is to it!

We can further improve the my_theme_the_header() function by adding a permission check to make sure the user can create a block before we add the link (EDIT: Permission check added to the code snippet above), and we can even add an edit link so site-owners can go to the header’s edit screen, but for the sake of this post we’re keeping things simple and open to your improvements.

Why this is important:

Using the technique above, existing themes can leverage the block editor for all things instead of being limited to the content area.

Using reusable blocks is a good way to get ready for Full-Site Editing and will be easier to transition to a new block structure. You can start exploring options, you can even start transitioning your existing headers to a block structure and use them as a fallback, combining them with the “Create a new header” link from the above example.

When FSE gets released, a theme that was previously using a reusable-block for its header will be a lot easier to transition to the new structure since the header will already be there in a proper blocks format.

Themes Team Meeting Agenda for July 7

The themes team conducts a meeting on the second and fourth Tuesday of the month, and once per month during the summer and holiday periods.

Along with the fixed agendas, we have an open floor at the end where you can ask or share anything related to themes.

We encourage all members and anyone interested to attend.

Channel: #themereview | Time: Tuesday, July 7 2020, 17:00 UTC

Meeting Agenda

  1. Weekly Updates
  2. Using reusable blocks to ease the transition to blockBlock Block is the abstract term used to describe units of markup that, composed together, form the content or layout of a webpage using the WordPress editor. The idea combines concepts of what in the past may have achieved with shortcodes, custom HTML, and embed discovery into a single consistent API and user experience. based themes
  3. Old themes with images that are not compatible with GPLGPL GPL is an acronym for GNU Public License. It is the standard license WordPress uses for Open Source licensing https://wordpress.org/about/license/. The GPL is a ‘copyleft’ license https://www.gnu.org/licenses/copyleft.en.html. This means that derivative work can only be distributed under the same license terms. This is in distinction to permissive free software licenses, of which the BSD license and the MIT License are widely used examples. due to license changes are blocked by Theme Check. What is the correct way to handle these images?
  4. How do we best handle updates to themes that has not been updated in two years?
  5. Date and time of the next meeting

The discussion about the meeting agenda can be held in the comments below. You are encouraged to propose topics.

Meetings usually last around 90 minutes.

Attend and share your valuable feedback and suggestions.

#agenda, #meeting, #themes-team

X-post: WCEU 2020 Online Contributor Day: Feedback and achievements

X-post from +make.wordpress.org/updates: WCEU 2020 Online Contributor Day: Feedback and achievements

Gutenberg + Themes: Week of June 29, 2020

Hi everyone! Here’s the eighth weekly roundup of theme-related discussions, fixes, and developments in GutenbergGutenberg The Gutenberg project is the new Editor Interface for WordPress. The editor improves the process and experience of creating new content, making writing rich content much simpler. It uses ‘blocks’ to add richness rather than shortcodes, custom HTML etc. https://wordpress.org/gutenberg/. This one’s coming a day early this week. 🙂

Since conversations are ongoing, some of the issues & PRs mentioned were also present in previous weeks. We’ve tried to put brand new ones at the top of each of the bulleted lists. 

Please weigh in on the tickets below — your voice and feedback are welcome!

In progress / Discussions:

General

  • Custom gradients cannot be edited if there are no explicit stops specified. 23501
  • Discussion around adding font unit options to the font size selector. 23323
  • Button BlockBlock Block is the abstract term used to describe units of markup that, composed together, form the content or layout of a webpage using the WordPress editor. The idea combines concepts of what in the past may have achieved with shortcodes, custom HTML, and embed discovery into a single consistent API and user experience.: Lighten the DOM in the editor. 23222
  • Limit display of block patterns if their included blocks are not available. 23275 
  • PR to adjust max-width setting for reusable blocks. 22632
  • PR for adding color controls to the list block. 21387
  • PR to change CSSCSS CSS is an acronym for cascading style sheets. This is what controls the design or look and feel of a site. specificity of default font-size rules. 22671
  • Fixes for the issue where custom theme colors don’t work in the editor after Gutenberg 7.9.1. 22356
  • Add a way to filterFilter Filters are one of the two types of Hooks https://codex.wordpress.org/Plugin_API/Hooks. They provide a way for functions to modify data of other functions. They are the counterpart to Actions. Unlike Actions, filters are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output. CSS classes for blocks using PHPPHP PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. http://php.net/manual/en/intro-whatis.php. 23223

Block-based Themes

  • Discussion about the support of shortcodes in block-based theme templates. 23626
  • Discussing the use of conditional logic in experimental-theme.json 22324
  • Tracking issue for Template Tags in Full Site Editing 22724 
  • Sync edits from theme to site editor 22469
  • Add Template lock to templates created in editor 23532

FSE Blocks

A set of blocks are available with the Gutenberg pluginPlugin A plugin is a piece of software containing a group of functions that can be added to a WordPress website. They can extend functionality or add new features to your WordPress websites. WordPress plugins are written in the PHP programming language and integrate seamlessly with WordPress. These can be free in the WordPress.org Plugin Directory https://wordpress.org/plugins/ or can be cost-based plugin from a third-party to enable building templates in FSE.

  • HTMLHTML HTML is an acronym for Hyper Text Markup Language. It is a markup language that is used in the development of web pages and websites. and CSS not present/inconsistent for FSE Blocks 22759
  • Suggestion for Site Title styling options. 23228
  • Group block is missing alignment options in the full-site editor. 23431
  • Add a Site Logo Block 18811

Global Styles

  • Discussion about presets for line-height, padding and margin. 23111
  • Tracking and consolidating style attributes for blocks. 22700 

Navigation

  • This board tracks Navigation Screen and Navigation Block issues that are important for the WordPress 5.5 releaseRelease A release is the distribution of the final version of an application. A software release may be either public or private and generally constitutes the initial or new generation of a new or upgraded application. A release is preceded by the distribution of alpha and then beta versions of the software.. Project Overview
  • There will be a new standing weekly meeting beginning 8 July in #core slack specific to the Navigation block and screen

Merged:

General

  • Fix centered legacy button blocks. 23381
  • Clarify the wp-block-styles documentation. 23359

Block-based Themes

  • Allow wide/full alignment in the full-site editor. 23488
  • Rename “Template Part” to “Section” in the full-site editor UIUI UI is an acronym for User Interface - the layout of the page the user interacts with. Think ‘how are they doing that’ and less about what they are doing.. 23295
  • Add theme exporter button to the full-site editor. 22922 
  • Template Part: Improve insertion flow 22395
  • Fix for issue where HTML file updates are not being reflected in the full site editor. 23591

General Resources:


Thanks to @itsjusteileen, @jffng, and @joen for help pulling this post together. Please let us know if it was helpful in the comments!

#gutenberg-themes-roundup

Block-based Theme Meeting Notes 1 July, 2020

This post summarizes the latest bi-weekly BlockBlock Block is the abstract term used to describe units of markup that, composed together, form the content or layout of a webpage using the WordPress editor. The idea combines concepts of what in the past may have achieved with shortcodes, custom HTML, and embed discovery into a single consistent API and user experience.-based Themes meeting (agenda, slack transcript). This meeting was held in the #themereview SlackSlack Slack is a Collaborative Group Chat Platform https://slack.com/. The WordPress community has its own Slack Channel at https://make.wordpress.org/chat/. channel on Wednesday, June 17, 2020, 12:00 PM EDT and was moderated by @kjellr.

GutenbergGutenberg The Gutenberg project is the new Editor Interface for WordPress. The editor improves the process and experience of creating new content, making writing rich content much simpler. It uses ‘blocks’ to add richness rather than shortcodes, custom HTML etc. https://wordpress.org/gutenberg/ Update

Gutenberg 8.4 has been released. This is the second-last releaseRelease A release is the distribution of the final version of an application. A software release may be either public or private and generally constitutes the initial or new generation of a new or upgraded application. A release is preceded by the distribution of alpha and then beta versions of the software. before WordPress 5.5. The full-site editor now includes an “export theme” option. This will download all of the block templates and template-parts that you’ve created or edited in the full-site editor. It’s really useful for creating block-based themes. This can eliminate a lot of copying and pasting when created block-based themes.

Export Tool in Gutenberg 8.4

Issues for Comments

The following issues are open for comments. If you’re looking to contribute to the next generation of theme creation, this is a good place to provide input.

  • Issue 23324 Conditional logic within experimental-theme.jsonJSON JSON, or JavaScript Object Notation, is a minimal, readable format for structuring data. It is used primarily to transmit data between a server and web application, as an alternative to XML.. This arose from a discussion in a previous meeting.
  • Issue 22759 HTMLHTML HTML is an acronym for Hyper Text Markup Language. It is a markup language that is used in the development of web pages and websites. output of all the full-site editing blocks (this is particularly useful for themes to cut down on CSSCSS CSS is an acronym for cascading style sheets. This is what controls the design or look and feel of a site.).
  • Issue 23228 Feature requests for the site title block.

External Resources

During a recent online event in conjunction with the Gutenberg Times @kjellr, @poena and @itsjusteileen discussed Full Site editing and Block-based Themes and @kjellr gave a demo of creating a theme from within the editor.

Discussion: Documentation

This week’s discussion centered around improving existing documentation that is currently available at

The team agreed the existing documentation is adequate to allow users to get started testing and experimenting with Full Site Editing for Block-based themes.

Some improvements could be:

  • to add a list of available features and the development of the status of those items.
  • adding a getting started section to the experimental-theme.json docs
  • linking to the Experimental Theme repo.
  • providing more links or fleshing out of what is a block template.
  • it is not clear how to include a template part in a template or if it is not templates and parts that are saved will be used if you switch themes.
  • how to create and edit templates needs to reflect the new export theme button. At should also note that it is recommend that you can edit things directly in the full-site editor.
  • include when exporting what to do with the post IDs that are created
  • the wp-admin/edit.php?post_type=wp_template screens, there should also be a note that those don’t reflect any templates that are bundled with the actual theme.
  • the the “Theme Blocks” section could list currently available FSE blocks but this will need to be updated. Everyone working in #core-editor should be notified to make sure PRs involving blocks for block-based themes are tagged in the repo.
  • an FSE tutorial can be added.

Open Floor

The theme experiments repo is open for submissions. Four themes are looking for feedback on open PRs that try implementing the theme.json

Two new issues were opened for creating and using templates in FSE: