An Introduction To The Sage WordPress Starter Theme

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.

@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-content\themes 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. ๐Ÿ˜€

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:

$ vendor\bin\sage meta
$ vendor\bin\sage config
$ vendor\bin\sage 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.


Did you find what you came for? Want me to cover something else? Let me know in the comments below! Share, if you liked this post! I’ll be covering Sage more in-depth in future blog posts, so stay tuned!

Leave a Comment

Your email address will not be published. Required fields are marked *