An Introduction To The Sage WordPress Starter Theme

WordPress

05/11/2019


WordPress is an awesome and powerful tool. It's not without reason that more than a third of all websites are powered by it. Unfortunately, its development workflow feels quite outdated and clunky when compared to PHP Laravel and many front-end frameworks. This can be mainly attributed to the core development team striving to never break backwards compatibility. πŸ™„ Fortunately, a group of talented developers, working under the company name of Roots, have taken it upon themselves to make WordPress development much more modern and streamlined. In this article, I'll be giving you an introduction to the Sage WordPress starter theme, their most popular project.

I'll be discussing what Sage has to offer, how to install it and how to read its folder structure.

What Sage offers

If you're coming from a more traditional web development background, you'll be familiar with some of the tools Sage offers.

First, Sage is based on Webpack. For those of you who are unaware of what it its: it's a module blunder. In layman's terms, it's a front-end tool that takes your interdependent assets (e.g. JavaScript, images, HTML, CSS, …) and processes it into something usable by the client aka website visitor. πŸ§β€β™‚οΈ This might involve processing Sass, checking JavaScript for errors, optimizing images, etc. But honestly, you rarely ever need to mess with Webpack, so it's something you may safely ignore for the time being.

Second, it comes preconfigured with Bootstrap, the most popular UI framework, making web design much easier for us grouchy developers. However, if Bootstrap isn't your jam, you can easily configure it with other frameworks such as Bulma, Foundation, etc. Or for the daring among us, you may not use one at all! 😱

Spiderman meme that says: Developer uses Bootstrap. You know, I'm something of a designer myself.

You wish... πŸ˜‰

Last but not least, Sage uses PHP Laravel's templating engine Blade for DRY (Don't Repeat Yourself) code. Now, while WordPress does provide DRY templating to a certain extent, I believe Blade's focus on creating more maintainable projects just make it a better option over its vanilla counterpart.

An example of the templating language can be seen below.

PHP
@extends('layouts.base')
@section('content')
@while(have_posts())
{!! the_post() !!}
@include('partials/content-single')
@endwhile
@endsection

Installation

Before you can install the Sage starter theme, you first need to make sure you meet the following dependencies in your development environment:

My preferred WordPress development environment uses Laragon, which comes with all these dependencies installed automatically. 😎 You may take a look at an article I wrote if you're interested in learning more about it!

Ahem, alright.... moving on. In your terminal, head over to the wp-contentthemes directory of your WordPress site. Then, execute the following command, replacing <your-theme-name> with a name of your choosing.

BASH
$ composer create-project roots/sage <your-theme-name>

Following this, Sage will be installed and you'll be prompted to answer several statements.

Regarding the prompt Path to theme directory (e.g., /wp-content/themes/tutorial) [/app/themes/sage], make sure to enter the path of your new theme, i.e. /wp-content/themes/<your-theme-name>. By default, it will suggest a different path that is used with one of Roots' other projects, namely Bedrock.

Last but not least, in your new theme folder, run yarn or npm install to install all the dependencies of your theme. Et voilΓ , the installation is complete! πŸ’ƒ

Aannndd don't forget, to run your site, type yarn start or npm run start.

TTY mode is not supported on Windows platform

For my fellow developers who are on Windows like I am, you'll likely encounter an error during the installation process: TTY mode is not supported on Windows platform. As a result, you won't be prompted any questions. However, if that's not the case for you, awesome! One less thing to worry about. πŸ˜€

Office space meme that says: Windows, if you could not be such a pain in the ass that would be great.

Windows strikes, again!

To solve this problem, cd into your newly generated theme and simply run the following commands one by one:

BASH
$ vendorbinsage meta
$ vendorbinsage config
$ vendorbinsage preset

Folder Overview

Using any new tool can be daunting at first, πŸ˜₯ which is why this section is dedicated to giving you an overview of Sage's project structure. To make this as digestible as possible, I'll go through each group of files one by one. And to make things more visually appealing, I'll be using the theme structure diagram from Roots' website.

App

TEXT
β”œβ”€β”€ app/ # β†’ Theme PHP
β”‚ β”œβ”€β”€ controllers/ # β†’ Controller files
β”‚ β”œβ”€β”€ admin.php # β†’ Theme customizer setup
β”‚ β”œβ”€β”€ filters.php # β†’ Theme filters
β”‚ β”œβ”€β”€ helpers.php # β†’ Helper functions
β”‚ └── setup.php # β†’ Theme setup

You can consider the app/ directory as the PHP logic πŸ‘©β€πŸ’» of your site. controllers/ contains your controllers... duh! But seriously, this is where your PHP logic belongs, e.g. sending a confirmation email after a visitor signs up for a newsletter. Now, the other files you can consider as a split up functions.php file. Instead of having all your code in one place, they are grouped separately instead. πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦

You'll find yourself visiting this folder once in a while.

Config

TEXT
β”œβ”€β”€ config/

The name is pretty self-explanatory. This folder takes care of your theme's configuration, e.g. namespacing or the path to your assets. Frankly, you'll rarely ever touch this folder.

Resources

TEXT
β”œβ”€β”€ resources/ # β†’ Theme assets and templates
β”‚ β”œβ”€β”€ assets/ # β†’ Front-end assets
β”‚ β”‚ β”œβ”€β”€ config.json # β†’ Settings for compiled assets
β”‚ β”‚ β”œβ”€β”€ build/ # β†’ Webpack and ESLint config
β”‚ β”‚ β”œβ”€β”€ fonts/ # β†’ Theme fonts
β”‚ β”‚ β”œβ”€β”€ images/ # β†’ Theme images
β”‚ β”‚ β”œβ”€β”€ scripts/ # β†’ Theme JS
β”‚ β”‚ └── styles/ # β†’ Theme stylesheets
β”‚ β”œβ”€β”€ functions.php # β†’ Composer autoloader, theme includes
β”‚ β”œβ”€β”€ index.php # β†’ Never manually edit
β”‚ β”œβ”€β”€ screenshot.png # β†’ Theme screenshot for WP admin
β”‚ β”œβ”€β”€ style.css # β†’ Theme meta information
β”‚ └── views/ # β†’ Theme templates
β”‚ β”œβ”€β”€ layouts/ # β†’ Base templates
β”‚ └── partials/ # β†’ Partial templates

Without a doubt, this is where you'll be spending most of your time. πŸ• The resources/ directory contains all your PHP pages, JavaScript, HTML, etc of your site.

First of all, the assets/ folder contains your images, fonts, JavaScript and CSS/SCSS files. The folder build/ you shouldn't touch at all and config.json/ can be safely ignored as you almost never need to edit it. On the other hand, you'll find yourself working in the scripts/ and styles/ folder quite often.

Next, views/ contains all your PHP view templates, or to be specific the Laravel Blade template files that Sage uses. As a result, you'll see your files ending with blade.php. It is common practice to use layouts and partials in Blade template development, hence the folders. The latter is similar to WordPress partials, too.

Apart from these two folders, there are a bunch of other files which you can safely ignore as well. Lot of ignoring, right? Well, you might want to change the screenshot.png image, so your WordPress theme has a nice cover. πŸ“·

Miscellaneous

TEXT
β”œβ”€β”€ composer.json # β†’ Autoloading for `app/` files
β”œβ”€β”€ composer.lock # β†’ Composer lock file (never edit)
β”œβ”€β”€ dist/ # β†’ Built theme assets (never edit)
β”œβ”€β”€ node_modules/ # β†’ Node.js packages (never edit)
β”œβ”€β”€ package.json # β†’ Node.js dependencies and scripts
└── vendor/ # β†’ Composer packages (never edit)

Lastly, I would call this the What? group, because that's the only thing you need to know about them. And by that I mean know nothing. πŸ‘‰ Insert obligatory "You know nothing Jon Snow" joke here...

If you ever do need to edit the composer.json and package.json files, there are plenty of tutorials out there on how to do so. These files aren't specific to the Sage starter theme and hence I won't go into them.


WRITTEN BY

Code and stuff