How I Rebuilt My Base Theme for Gutenberg

WordCamp Philly 2019

Beth Soderberg | @bethsoderberg

How to Build a Starter Theme and Optimize it for Gutenberg

  1. What is a "starter" or "base" theme?
  2. Why would you use one?
  3. How do you build one?
  4. What makes a theme Gutenberg-compatible?
  5. Once you build one, what next?

What is a "starter" or "base" theme?

What is a theme?

What is a "starter" or "base" theme?

It establishes defaults.

There is no/minimal styling.

You're not supposed to make a child theme.

It's meant to be "hacked".

Why would you use one?

Automation of your default setup.

Efficiency!

Built-in best practices/expertise from the community.

Common starting points make finding/getting help easier.

Sample paths to using a base theme:

  1. tweak existing themes with customizer
  2. child themes
  3. base themes
  1. page builders
  2. page builders + customizer
  3. child themes
  4. base themes
  1. frameworks
  2. child themes
  3. base themes

How do you build one?

Don't roll your own.

Choose a Base for your Starter Theme

My Starter Theme Choices

Goals

  • 2016: a default to work from
  • 2019: the same thing but Gutenberg-compatible
  • 2020: ideal workflow and theme structures

Theme evolution.

Predecessors to _s:

  • Kubrick
  • The Sandbox
  • Thematic
  • Twenty Ten
  • Toolbox
  • Twenty Eleven

Themes based on _s:

  • Twenty Twelve
  • Twenty Thirteen
  • Twenty Fourteen
  • Twenty Fifteen
  • Twenty Sixteen
  • Twenty Seventeen
  • Gutenberg Starter Theme
  • Varia

Twenty Nineteen Theme based on:

  • _s
  • Gutenberg Starter Theme

Determine default template files.

Default Template Files

_s

  • index.php
  • archive.php
  • single.php
  • page.php
  • 404.php
  • search.php

2016

  • kept _s defaults

2019

  • added front-page.php

Default Page Component Templates

_s

  • comments.php
  • header.php
  • footer.php
  • sidebar.php

2016

  • kept _s defaults

2019

  • removed sidebar.php

Default Template Parts

_s

  • content-none.php
  • content-page.php
  • content-search.php
  • content.php

2016

  • kept _s defaults

2019

  • added content-home.php

Decide if you have adjustments to functions and includes.

Make sure you're curating administrative files.

Kept

  • LICENSE
  • README.md
  • readme.txt
  • screenshot.png

Removed

  • phpcs.xml.dist
  • .editorconfig.org
  • CODE_OF_CONDUCT.md
  • CONTRIBUTING.md
  • CONTRIBUTORS.md

Remove Unused Directories & Files

2016

  • ?

2019, cont.

  • assets directory
  • css/dark-mode.css
  • style-editor-dark.css
  • inc/jetpack.php
  • languages/beth.pot
  • languages/readme.txt
  • rtl.css
  • remove calls to these files from functions.php

Modify Text/Content Defaults

Add a .gitignore


                        **/.sass-cache
                        **/.sass-cache/*
                        .sass-cache
                        .sass-cache/*
                        .DS_Store
                        _/img/.DS_Store

                        # ignore Editor files
                        *.sublime-project
                        *.sublime-workspace
                        *.komodoproject
                    

Fix Annoying Things

Examples, mostly from 2019:

  • missing text styling defaults
  • changing call to logo in header
  • adding front-page.php default
  • changing link style defaults
  • unify margin/padding patterns around elements
  • adding images directory
  • more text default changes

Determine approach to styling.

Select default units of measurement.

  • _s: ems
  • me: rems

Borrow conventions from other places.

Styling Logic from Drupal Themes/Developers


/* Setting base font size as 10 for easy maths */
$base-font-size:   10px; /* The font size set on the root html element */
$base-line-height: 24px; /* The base line height */

/* Font stacks */
$times:           Times, "Times New Roman", Georgia, serif;
$arial:           Arial, Helvetica, sans-serif;
$courier:         "Courier New", monospace, sans-serif;

/* Font stack variables */
$font: $arial; /* The font family set on the html element. */
$font-code: $courier;
$font-pre: $courier;
$serif: $times;
$sans-serif: $arial;
$monospace: $courier;

/* Font weights */
$light: 200;
$regular: 400;
$bold: 700;
$black: 900;
                    

Customize Sass Architecture

Default _s

2016 & 2019

Revise CSS Approach Over Time

2016

2019

2016

2019

2016

2019

2016

2019

If you're using Sass you need to compile it.

  • 2016: Compass
  • 2019: Compass
  • 2020: Gulp

Why Compass?

  • Always used to compile CSS
  • Sometimes used for vendor prefixing
  • Intended to use for image sprites...

My config.rb file


            require 'compass'

            http_path = "/"
            css_dir = "/"
            sass_dir = "sass"
            images_dir = "images"
            javascripts_dir = "js"

            output_style = :expanded 
            # or :nested or :compact or :compressed

            relative_assets = true

            environment = :development
            #environment = :production

            output_style = (environment == :development) ? :expanded : :compressed

            relative_assets = true

            sass_options = (environment == :development) ? {:debug_info => true} : {}

            sourcemap = true
            line_comments = true
        

Adding Gulp in 2020. Why not Grunt?

Data from NPM Trends.

I need a task runner that:

What makes a theme Gutenberg-compatible?

Default CSS for Gutenberg Blocks

# General Structure
  ## Code
  ## Cover
  ## Embeds
  ## Gallery
  ## Group
  ## Image
  ## Latest Posts
  ## List
  ## More
  ## Pullquote
  ## Quote
  ## Separator
  ## Table
  ## Video
# Additional Theme Styles
  ## Color Palette

Expanding Theme Support


        /* Add core block visual style support */
        add_theme_support( 'wp-block-styles' );

        /* Add full and wide align images */
        add_theme_support( 'align-wide' );

        /* Add responsive embed support */
        add_theme_support( 'responsive-embeds' );
    

Adding Color Palette Defaults


        add_theme_support( 'editor-color-palette', array(
            array(
                'name'  => __( 'Raspberry', 'beth2' ),
                'slug'  => 'raspberry',
                'color' => '#ca0164',
            ),
            array(
                'name'  => __( 'Dark Gray', 'beth2' ),
                'slug'  => 'dark-gray',
                'color' => '#444',
            ),
        ) );
                    

Add Color Palette Default CSS


        .has-raspberry-color {
            color: #CA0164;
        }

        .has-raspberry-background-color {
            background-color: #CA0164;
        }

        .has-dark-gray-color {
            color: #444;
        }

        .has-dark-gray-background-color {
            background-color: #444;
        }
                    

Adding Block Font Size Defaults


        add_theme_support( 'editor-font-sizes', array(
            array(
                'name' => __( 'Small', 'beth2' ),
                'size' => 12,
                'slug' => 'small'
            ),
            array(
                'name' => __( 'Normal', 'beth2' ),
                'size' => 16,
                'slug' => 'normal'
            ),
            array(
                'name' => __( 'Large', 'beth2' ),
                'size' => 36,
                'slug' => 'large'
            ),
            array(
                'name' => __( 'Huge', 'beth2' ),
                'size' => 50,
                'slug' => 'huge'
            )
        ) ); 
        

Add Font Size Default CSS


        .has-huge-font-size {
            font-size: 5rem;
        }
        .has-large-font-size {
            font-size: 3.6rem;
        }
        .has-regular-font-size {
            font-size: 1.6rem;
        }
        .has-small-font-size {
            font-size: 1.2rem;
        }
        

Prevent Editor Independence


        /* Disable custom colors */
        add_theme_support( 'disable-custom-colors' );

        /* Disable custom font sizes */
        add_theme_support('disable-custom-font-sizes');
        

Add support for editor styling

add_editor_style( 'style-editor.css' );
  • Add style-editor.css to root of theme
  • Write default styles to customize admin theme experience

Once you build one, what's next?

Monitor and Adjust

Version control your work with git.

Keep notes on planned changes.

  • specific: move blocks.css into Sass partials and rewrite those defaults.
  • general: create custom starter plugin

Monitor Gutenberg plugin for future needs.

Monitor specific Gutenberg ideas.

Do I need a starter js/editor.js file?


            wp.domReady( () => {
            wp.blocks.unregisterBlockStyle( 'core/button', 'default' );
            wp.blocks.unregisterBlockStyle( 'core/button', 'outline' );
            wp.blocks.unregisterBlockStyle( 'core/button', 'squared' );

            wp.blocks.registerBlockStyle( 'core/button', {
                name: 'cta',
                label: 'Call to Action',
                isDefault: true,
            } );

            wp.blocks.registerBlockStyle( 'core/button', {
                name: 'sign-up',
                label: 'Sign Up',
            } );

            wp.blocks.registerBlockStyle( 'core/button', {
                name: 'see-more-link',
                label: 'See More Link',
            } );

        } );
            

Don't forget to evaluate non-Gutenberg adjustments.

Pay attention to how you/your team works to optimize your theme for YOU.

Keep it simple!

Thank you!

@bethsoderberg

bethsoderberg.com

Possibly Useful Links