turned on gray laptop computer

How to Start a WordPress Block-Based Template from Scratch

The future of WordPress theme development is here, and it’s all about blocks! Gone are the days of complex PHP templates and rigid structures. Block themes empower you with unprecedented flexibility and control over your website’s design directly within the WordPress editor.

Ready to dive in and experience this exciting new era? This tutorial will guide you step-by-step through the process of creating your very own WordPress block-based theme from scratch. Don’t worry if you’re new to this – we’ll break it down into manageable chunks.

What You’ll Learn:

  • Understanding the fundamental structure of a block theme.
  • Creating essential theme files like theme.json and index.html.
  • Defining theme support for key block editor features.
  • Building basic templates using HTML and block patterns.
  • Styling your block theme with CSS.
  • Tips for further exploration and customization.

Prerequisites:

  • Basic understanding of HTML and CSS.
  • Familiarity with the WordPress interface.
  • A local WordPress development environment (like LocalWP, XAMPP, or MAMP) is highly recommended.

Let’s Get Started!

Step 1: Setting Up Your Theme Directory

First, navigate to your WordPress installation’s wp-content/themes/ directory. Create a new folder for your theme. Let’s call it my-first-block-theme.

Step 2: The Essential style.css File

Every WordPress theme needs a style.css file in its root directory. This file provides WordPress with basic information about your theme. Open your text editor and create a style.css file inside your my-first-block-theme folder with the following content:

/*
Theme Name: My First Block Theme
Author: Your Name
Author URI: https://yourwebsite.com
Description: A basic block-based theme created from scratch.
Version: 1.0
Requires at least: 5.8
Requires PHP: 7.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: my-first-block-theme
*/

Replace the placeholder information with your own details. The Text Domain is important for internationalization.

Step 3: The Revolutionary theme.json File

This is the heart of your block theme! The theme.json file in the root directory configures various aspects of your theme, including default styles, block settings, and editor features. Create a theme.json file with the following basic structure:

{
	"$schema": "https://schemas.wp.org/trunk/theme.json",
	"version": 2,
	"settings": {
		"color": {
			"palette": [
				{
					"slug": "primary",
					"color": "#007bff",
					"name": "Primary"
				},
				{
					"slug": "secondary",
					"color": "#6c757d",
					"name": "Secondary"
				},
				{
					"slug": "light",
					"color": "#f8f9fa",
					"name": "Light"
				},
				{
					"slug": "dark",
					"color": "#343a40",
					"name": "Dark"
				}
			],
			"gradients": []
		},
		"typography": {
			"fontFamilies": [
				{
					"slug": "system-sans",
					"name": "System Font",
					"fontFamily": "-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Oxygen-Sans, Ubuntu, Cantarell, \"Helvetica Neue\", sans-serif"
				}
			],
			"fontSizes": []
		},
		"layout": {
			"contentSize": "700px",
			"wideSize": "1200px"
		}
	},
	"styles": {
		"blocks": {}
	},
	"templateParts": []
}
  • $schema: Points to the official theme.json schema for validation.
  • version: Indicates the theme.json schema version.
  • settings: Configures theme support for features like color palettes, gradients, typography options, and layout settings. We’ve defined a basic color palette and layout here.
  • styles: Allows you to define default styles for blocks.
  • templateParts: Registers reusable parts of your theme (like headers and footers).

Step 4: The Minimal index.html Template

Unlike classic themes that rely heavily on PHP files, block themes use HTML files to define their templates. Create an index.html file in your theme’s root directory with the following basic structure:

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
	<meta charset="<?php bloginfo( 'charset' ); ?>">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="profile" href="https://gmpg.org/xfn/11">
	<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
	<?php wp_body_open(); ?>
	<header class="site-header">
		<h1><?php bloginfo( 'name' ); ?></h1>
		<p><?php bloginfo( 'description' ); ?></p>
	</header>

	<main class="site-content">
		<div class="wp-block-query">
			<article <?php post_class(); ?>>
				</article>
			</div>
		</main>

	<footer class="site-footer">
		<p>&copy; <?php echo date( 'Y' ); ?> <?php bloginfo( 'name' ); ?></p>
	</footer>
	<?php wp_footer(); ?>
</body>
</html>
  • HTML Structure: This is a standard HTML5 document.
  • WordPress Functions: You’ll notice familiar WordPress functions like language_attributes(), bloginfo(), wp_head(), body_class(), wp_body_open(), post_class(), wp_footer(). These are still used to integrate WordPress functionality.
  • Block Markup: The comments like and represent block markup. This is how you tell WordPress to insert dynamic content using blocks within your templates. The wp:query block is used to display a list of posts.

Step 5: Activating Your Theme

Now, go to your WordPress admin dashboard, navigate to Appearance > Themes, and you should see “My First Block Theme”. Activate it.

What You’ll See:

At this point, your website will likely look very basic, with a simple header displaying your site title and description, followed by a list of your blog posts (if you have any), and a basic footer. This is because we haven’t added any custom styling yet.

Step 6: Adding Basic Styling (optional but recommended)

Create a style.css file (if you haven’t already) in your theme’s root directory and add some basic CSS to style the elements:

/*
Theme Name: My First Block Theme
Author: Your Name
Author URI: https://yourwebsite.com
Description: A basic block-based theme created from scratch.
Version: 1.0
Requires at least: 5.8
Requires PHP: 7.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: my-first-block-theme
*/

body {
	font-family: sans-serif;
	line-height: 1.6;
	margin: 0;
	padding: 0;
}

.site-header {
	background-color: #f0f0f0;
	padding: 20px;
	text-align: center;
}

.site-content {
	max-width: 960px;
	margin: 20px auto;
	padding: 20px;
}

article {
	margin-bottom: 30px;
	border-bottom: 1px solid #ccc;
	padding-bottom: 20px;
}

.site-footer {
	background-color: #333;
	color: #fff;
	text-align: center;
	padding: 10px;
	position: fixed;
	bottom: 0;
	width: 100%;
}

Save the style.css file and refresh your website to see the basic styling applied.

Further Exploration:

This is just the beginning! Here are some things you can explore next:

  • Creating More Templates: Add files like home.html, single.html, page.html, etc., to customize how different types of content are displayed.
  • Template Parts: Create template-parts/header.html and template-parts/footer.html and include them in your main templates using the “ block.
  • Block Patterns: Define reusable sets of blocks in a patterns/ directory to provide users with pre-designed layouts.
  • Custom Block Styles: Allow users to apply custom styles to specific blocks within the editor using the styles section in theme.json.
  • Custom Block Templates: Define specific block arrangements for certain post types or templates.
  • Theme Support Features: Explore the various settings in theme.json to enable features like featured images, custom logo, menus, and more.

Conclusion:

Congratulations! You’ve taken your first steps into the exciting world of WordPress block theme development. By understanding the fundamental role of theme.json and using block markup in your HTML templates, you have the power to create highly customizable and flexible themes. Keep experimenting, explore the WordPress developer documentation, and embrace the block-based future!

What are your thoughts on block themes? Have you started building with them yet? Share your experiences and questions in the comments below!