Bootstrapping WordPress Theme Development with Sage
If you are familiar with WordPress theme development, you know things can get a little messy when you are starting from the ground up. Building a theme from scratch is not easy, and setting it up can take a lot of the time you have, even before you start to write actual code. Because of this, developers have built starter themes, which are themes with code to display posts, pages or anything, but they lack on design and have a minimal layout. This allow us to not worry that much about how to bring content from the database so we can focus from the beginning on its presentation.
There are some key points you need to be aware of when choosing a starter theme: documentation, support, technical features and maintenance. You will need documentation and support when you are learning to use a theme, and when you have started developing and need troubleshooting the errors you might make. You need to know what tools are included in the starter theme, if it is compatible with browsers and your own tech stack, and if it suits your project purposes. Also, you want it to be updated constantly, so you can be sure you are up to date with the standard tools and your theme stays compatible with newer versions of WordPress all the time.
Here at Swapps we use Sage, a starter theme developed by Roots based on HTML5 boilerplate and it comes with features that help us to create new themes in a fast and clean way. It includes Bower, Gulp, Browsersync and Bootstrap, defining a workflow that you can use to build your theme. In case you don’t know what these tools are for, I’ll give you a short description:
- Gulp takes care of common frontend tasks and does them for us. That includes, but is not limited to, minifying CSS and JavaScript files, compiling CSS preprocessors, image optimization, concatenation of files and code linting.
- Bower is a front-end package manager.
- Bootstrap is a widely used CSS framework made by Twitter engineers, it allows us to create responsive, good-looking sites using predefined classes in short time.
- Browsersync synchronizes file changes in multiple browsers and devices (we don’t have to reload every time we save a change).
Note: new version (Sage 9) uses Webpack instead of Gulp and relies on npm instead of Bower for managing dependencies.
Sage has strong documentation and a forum where the community or even the Sage developers themselves can solve any problem you might have. They also include a README file into the project that you can use to get started. The Sage team is also aware of new technologies and trends in web development, i.e. using Webpack in their new release as I said above, so you can be sure that a theme based on Sage will always be updated with the best technology at the moment.
Installation
So, how do we install it? First make sure you have these installed:
- PHP 5
- Composer
- Node.js
- Git
You need to install Gulp and Bower in order to use them, but this is easily done with Node.js using npm. Just type npm install -g gulp bower
in your console.
Go to your WordPress themes directory, at this point we have two ways to install Sage. First one is cloning the repository:
git clone https://github.com/roots/sage.git theme-name
Other way is through Composer, using this command:
composer create-project roots/sage theme-name 8.5.0
Either way, don’t forget to change theme-name with the name you want for your new theme. Also, you can download the Git repository as a .zip file and extract it in your theme but we prefer automated things, right?
File structure
You can see the common WordPress files (index.php, functions.php, single.php, page.php, etc) at the root of your theme folder. There we have the lib directory, where the specific Sage configuration is stored. There is also the assets directory, where we can put our images, styles, scripts and fonts. The templates directory contains files for all templates we commonly see in a WordPress theme, the key difference being they are based on HTML5 and they have improved accessibility using ARIA roles. dist folder contains the processed assets that are going to be used in our live site, they are the result of the automatized workflow and should never be edited. Sage is multilingual ready, so we have a lang folder containing a file named sage.pot, in case you need translations.
Customization
You can setup your Sage installation editing the files included into the lib folder. There are some files serving different purposes:
- assets.php, here is where styles and scripts are enqueued.
- extras.php contains functions for adding classes to
and a link to continue reading at the end of posts excerpt.
- setup.php is where the main configuration is stored, you can enable/disable theme support for WordPress features, register sidebars and navigation menus, and choose in which pages do you want to display a sidebar (or not)
- titles.php controls the output of page titles.
- wrapper.php has the functionality for Theme Wrapper
The basic setup is done in setup.php, here are some snippets of the most essential features.
// lib/setup.php add_theme_support('title-tag'); // Enable modifying the title tag on your web page. add_theme_support('post-thumbnails'); // Enable WordPress featured images. // Add navigations, a primary navigation is registered by default. register_nav_menus([ 'primary_navigation' => __('Primary Navigation', 'sage') ]); // Types of posts enabled by default. Modify this as you need. add_theme_support('post-formats', ['aside', 'gallery', 'link', 'image', 'quote', 'video', 'audio']); // Enable support for HTML5 markup in the sections listed. add_theme_support('html5', ['caption', 'comment-form', 'comment-list', 'gallery', 'search-form']); // Specify path of the stylesheet for customizing the TinyMCE Editor used by WordPress. add_editor_style(Assets\asset_path('styles/editor-style.css')); // Here you can add sidebars to your theme. By default, Sage comes with Primary and Footer sidebars. function widgets_init() { register_sidebar([ 'name' => __('Primary', 'sage'), 'id' => 'sidebar-primary', 'before_widget' => '