Justin Tadlock

Do we need advanced starter themes?

Screenshot of the Atom editor with a file from the Mythic starter theme.

Theme authors dont need new advanced starter themes. The most common issues in themes submitted to http://wordpress.org are still the basics: licensing, escaping, translations, and including script and styles. Encourage themers to learn the basics first.

Otherwise they will not understand the code they are including in their themes. If you are working on a starter theme, please look at what theme authors really need help with. Because it is not the build process.

~ Carolina Nymark

It’s a sentiment that I’ve heard before. I’ve addressed it to a degree on the Theme Hybrid blog last year. But, I mostly think it’s a misunderstanding of what this new crop of starter themes brings to the table.

I largely agree with the notion that the most common issues in themes submitted to the official WordPress theme repository cannot be addressed by simply providing more “advanced” code. However, there’s still a place in the WordPress ecosystem for developers who need more than the basics.

I struggle with the term “advanced” in this context. What we’re really talking about is utilizing newer and modern features of Web development. “Modern” does not necessarily have to equate to “advanced,” lest we run afoul of taking for granted everyday features not available 10 years ago. While modern starter themes are not the answer for everyone, they can improve development in many ways.

In this post, I’m going to address some of the concerns brought up in the Twitter thread quoted above and cover some of the reasons why I believe modern starters can be advantageous.

Also, I’m writing this post from the viewpoint of my own starter theme, Mythic. I cannot say whether all other modern starters handle things in the same way.

Translations

Mythic utilizes the Node WP i18n project to handle the translation process. Here’s a few of the things it does:

  • Recognizes the textdomain defined via the Text Domain header in style.css.
  • Adds textdomains where they’re missing.
  • Replaces incorrect textdomains in use.
  • Builds the POT file for translators.

The theme simply offers a wrapper command for this to simplify the process to the point where the theme author doesn’t need to figure out how to configure it. They just type in a command and watch the magic happen.

Of course, Mythic also wraps all of its text strings in the appropriate translation functions.

Escaping and sanitizing

Many basic starter themes just give you a jumping off point. What you do with the code after that is up to you. With Mythic, that’s not the case. By using its built-in tools, it can help you build better themes beyond the starting line.

Mythic integrates directly with the WP Theme Review Coding Standards. With a one-line command, it will analyze all of the theme’s files and spit out the results, pointing out the exact file names and line numbers where there are issues. It does this without the theme author having to learn how to configure an extra tool or use another plugin.

This is as good as it gets for a starter theme addressing common issues such as escaping on output and sanitizing on input. Outside of leading by example within the theme code, there’s little more that any starter can offer.

Mythic also has style and JS linting built in, so it can let you know when you’ve done something wrong in those areas.

Modern PHP

While Mythic currently has a minimum PHP version of 5.6, I plan to bump that beyond PHP 7 at some point in the future. This is important because taking advantage of some newer PHP features means the code is better structured and clear.

For example, type and return type declarations improve code by making sure accepted and returned data are of the appropriate type. Let’s look at a basic math class with an addition method:

class Math {
	public static function sum( int $x, int $y ) : int
	{
		return $x + $y;
	}
}

The Math::sum() method takes two parameters: $x and $y. It only allows an integer to be passed in for either of those. And, because we set a return type declaration of int, we’ll only ever get an integer back. Using modern techniques like this makes your code much better. In many cases, it makes it safer because you know 100% for sure what type of data to expect.

This is just a small sample of how using more modern features can make theme coding better. PHP is becoming a nicer language to work with.

Prefixing / Namespacing

One of the major problems for a long time (and it’s still sometimes an issue) is developers not prefixing their function and class names.

With Mythic, that’s a non-issue. Because we use namespaces out of the box, developers must learn how namespacing works to even build with the theme. Otherwise, they’ll get fatal errors during the development process. There’s no getting around it because of the way the theme is set up.

Forcing theme authors to use namespacing by nature of the system design instantly solves one common issue.

There’s a few folks on the WordPress theme review team who have an aversion to namespaces, a feature implemented in PHP 5.3. It’s time to learn. The standard way of “prefixing” is baked right into the PHP language.

Composer

Mythic teaches you how to use Composer, which is the standard used by nearly the entire PHP world outside of the WordPress bubble. Composer allows you to pull in packages when you need them instead of reinventing the wheel or copying/pasting third-party libraries into a theme sub-folder.

Here’s a few of the packages that I offer for theme dev that work along with the starter:

Code reusability is one of the cornerstones of smart development. There’s no need to reinvent the wheel when there’s an existing project that already covers your needs with other folks maintaining the code base and fixing bugs.

But, you don’t really get the benefits of those things without using more modern tools.

The build process

It’s a mistake to not look at the build process as something that can help address common theme development issues. Build scripts are powerful tools that can improve code if you use them.

It took nearly a year to create the Mythic starter theme. My mindset throughout the entire process was improving theme code for the next 10 years. A major part of that was looking at how the build process could help theme authors with common problems. The other devs who worked on the project and I spent more time refining the build process than any other piece of the puzzle.

Why? Because we believe in efficiency and following standards.

Like it or not, build tools are a part of the modern development workflow. And, they can actually improve code.

WordPress.org vs. literally everywhere else

While I’ve coded Mythic to play nice with WordPress.org’s rules—some of which are unnecessary hindrances to improving how theme authors build upon the platform—it’s only a tiny fraction of the themes that exist in the world. The best theme authors I know are not submitting themes there. They’re building custom sites for clients. Mythic’s primary audience is for those developers.

It’s easy to look at the problems faced by the WordPress theme review team and think of those issues as universal. And, perhaps they are when talking about free and commercial themes. However, in my experience, these issues are far less common with the types of developers who’d use Mythic for their client builds.

If we want developers to be better, I recommend challenging them to learn modern techniques, tools, and features. Many of them force you into coding to standards by design.

Variety is the spice of life

The Underscores theme is a nice jumping off point for someone just getting into theming. It’s the de facto standard for themes that come into the official repository. It’s done wonders for making theme code better over the years. Even I encourage first-time theme authors to use it rather than my starter.

But, once you need to tackle some more advanced things, you may find that you need a bit more.

That’s where Mythic and other modern starters come in. They offer that upgraded developer experience that’s lacking from “simpler” starters.