Some WordPress Background Information
Before we dive into the tutorial proper, it’s helpful to know a little general background information about what themes are and how they interact with the WordPress core. If this section is trivial to you, I apologize, but this is the most detailed WordPress theme tutorial in the world… what were you expecting?
Magnets and WordPress… How do they Work?
We all know the broad strokes of what a WordPress is and what a theme does. But a more precise understanding of exactly how that happens behind the scenes will be invaluable when you start coding your first theme.
Any given snipper of WordPress code falls into one of three categories. It’s either a (1)core function, a (2)theme, or a (3)plugin. If you don’t mind, I think it’s extremely worth clarifying what exactly these do, do not, and should never mean.
1. The Core
This is the core WordPress code, often referred to simply (but lovingly) as “the core.” Indeed it is exactly that, the heart of the WordPress system. A semi-obvious but important concept to understand is that the core code does not change! (Unless you hack it manually). Themes and plugins don’t alter the core; rather, the core is coded in such a way that themes and plugins can add or change some data offered up by the core… after the core is done with it, of course.
To put it another (perhaps more abstract) way, the core code asks the theme what it should do in certain situations. A theme can only do what the core allows it to, but thankfully the core gives a lot of leeway in this area. Developing an understanding of which functions belong to the core and whether the rest is plugin or theme territory will help make better sense of the overall process and just plain make better themes.
Essentially, any code that doesn’t phycially belong to the core can rightly be called a plugin, even themes (I know that’s confusing, sorry). Strictly speaking, themes are really just very elaborate, design oriented plugins. But please don’t get hung up on this definition because while illustrative of the relationship, in WordPress parlance “Plugin” and “Theme” mean two very different things, and mixing up the terms may get you fingered as a hapless noob.
Plugins typically focus on adding a single, or narrow band of, functionality to a WordPress site. While they can alter the way things look, their existence is pointed towards modifying the standard WordPress behavior in some way, not systematically altering the display. Your average plugin does things like add social media buttons, tracking codes, display various novel widgets in the sidebar, etc.
Like the core, the very name is instructive of what it is: code you PLUG IN to the core. Going back to our abstract example: in the
process of rendering pages, the core pauses at various points in the code and “asks” if the active plugins if they have anything to add. These pauses are known as hooks. We call them that because these are the places the core allows plugins and themes to “hook into” to a page.
We will examine hooks in their own right a little later on in this tutorial, I only introduce them now because I think they help to really crystallize the relationship between the core and a theme. While the core gives plugins and themes a “stage” on which they can do or say pretty much whatever they want, it still decides when and where that stage is, and controls the action enough to make sure no one gets hurt… usually.
A theme is a set of files containing code that gives the core and the browser very specific instructions on how every public page of a WordPress site should look, down to the tiniest detail. It goes way beyond the typical scope of "themes" generally, which are usually just a way to change the color and font of some set interface. But WordPress has elevated themes to a much higher station. Themes don’t just descide how data is display, it decides what data, where, in what order, and that’s before you even break out the big PHP code guns.
The essential distinction is that plugins exist to add functionality, whereas themes exist to add style! *jazz hands*
While themes are technically capable of doing everything plugins can do (and indeed you may want to include some special plugin-like functionality to complement a feature in your theme), you should try to keep your theme focused on how pages display, not how pages act. While there is no official rule that themes need to stay out of plugin territory, this is the prevailing view, and for good reason:
You can concurrently activate as many plugins as you want, but you can only run one theme at a time. Themes that come bundled with plugin-like baggage present the admin with an ultimatum that is sometimes frustration and always unnecessary: “Run all these pseudo-plugins or don’t use this theme.” While some theme-related functionality can be a great thing, if it strays too far from it’s purpose, it will start turning people off to your theme.
My guiding principle on where to draw the line is this:
if a functionality in your theme could stand alone as its own plugin, then it should. That way, those who want the functionality can have it, and those who don’t, aren’t forced to.