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! π±
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.
@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.
$ 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. π
To solve this problem, cd
into your newly generated theme and simply run the following commands one by one:
$ 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
βββ 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
βββ 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
βββ 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
βββ 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.
- Laragon WordPress Development Environment - The Hidden Gem
- How To Install PHPUnit for WordPress on Windows