themes

I have this legacy WordPress theme (Arras). Over the years I’ve made several attempts at refactoring it. My first attempts were back in 2013. Those early attempts fixed a few of the pressing issues, but were, I confess, less than satisfactory. The code-base was still pretty messy, and my changes broke as many things as it fixed. It happened that the things that broke weren’t parts of the theme I myself was using, so the new mess was, at the time, more acceptable than the old mess.

Eventually, the mess became too much and I consigned the project to “no longer actively maintained” status. (I’m pretty sure the difficulties were what led the original developer to abandon it as well.)

This week I spent a bit of time with it again, coming back to it after a long hiatus with fresh eyes and a couple more years of experience. I started in on the project from where I’d left off before and made a bit of headway. The experiments this week have given me some hope that the mess can be cleaned up.

That said, I’m going to start back again on the project from where I first inherited it. With any luck, the refactoring from the beginning can become a kind of learning tool and the theme can be restored, bit by bit, to a useful state. Starting again from before my previous refactoring will also make it easier, this time around, to retain the functionality it had at one time that was lost in my previous attempts.

Lee Hurst gave a demo of Google Data Studio that opened up a huge new horizon for me.

WordPress Themes and MVC (or lack thereof)

I’ve been thinking about WordPress themes lately: cleaning up a legacy theme in my spare time, watching the new Beans theme framework develop, growing more dissatisfied with the WordPress “spaghetti and meatballs” in general. Matt’s presentation, a kind of 35,000 ft view of opportunities for implementing a more MVC pattern within WordPress made a lot of sense and pointed towards a few patterns to try in my future exploration.

CSS Grid

Since Eric Meyers CSS Definitive Guide, 4th edition dropped last month, I’ve had my mind on the extent to which that technology has changed even in just the past year or so. Most notably, the new Grid specification has finally been fully implemented in the MS Edge browser — which makes it realistic to actually use it on new sites. But the Definitive Guide is a huge book, and I’d been on the fence about whether to actually dive in. I’d dabbled with Grid over the last couple months, but Tracy’s demo convinced me that now is the time to take the plunge.

Google Data Studio

OMG. You mean I can import whatever data I want however I want into a widget without coding the whole damn API myself! Holy crap! This changes everything. Big thanks to Lee for fleshing out what all the commotion is about.

What’s Next

I’ve realized that I, too, have a few things I’ve learned over the years that I need to be more proactive about sharing. So the last thing about the WC Rochester experience left me with is the itch to submit a talk or two of my own for a few upcoming WordCamps. Object Oriented WordPress Themes anyone?

As I’ve been tinkering with my Arras theme over the years, one of the things that I keep coming up against is the odd way that WordPress handles templating. It makes accessing objects in theme templates a less-than-intuitive process.

Essentially, there are two ways that WordPress templates get included into a webpage:

The Main template (like index.php, page.php, single.php and all their ilk) are included in the global scope via the template-loader.php file.

Sub-templates (like header.php, sidebar.php, footer.php along with any other templates that get called from the main template via get_template_part()) are included in the scope of a call to the locate_template() function.

What that means for themes is that there isn’t a consistent way to access theme variables in templates.

For the main templates, you can, in your theme setup, declare a global variable (yuk), and use it in your global templates:

But to use the same global variable inside a sub-template, I need to declare global in each of the sub-templates.

header.php

global $theme_variable;
...
echo $theme_variable;

So, what’s the big deal? It’s only one little thing in the sub-templates. But if you plan on distributing your theme, it’s not obvious to someone looking at the code (who is probably not so familiar with WP templates) why it’s in some template files but not others. Even seasoned WP developers are going to have to think it through to get to “Oh, yeah, that’s why.”

Another hack for getting your variable into the sub-templates is to insert it into WP’s $wp_query global variable. Here’s how:

For it to work, you have to hook your variable injection into $wp_query after the query has been set up. (That’s why, above, I’ve hooked it to the wp hook.) It works because locate_template() extracts all the query variables within the $wp_query global object into it’s own scope before including the template. So now you can call your variable in sub-templates without declaring (yet another) global variable:

header.php

echo $theme_variable;

Unfortunately, though, the template-loader.php file doesn’t extract the $wp_query variables before calling your main template, so you can’t access your theme variable in any of your main templates.

I suppose you might implement both the global and the $wp_query insertion together.

Both of the preceding methods are ultimately hacks. WordPress calls the theme’s functions.php file functions, after all, because it comes from a time when PHP was a purely procedural templating language. As such, WordPress is intended for you to handle your theme’s business using functions, not variables and objects. Functions in the theme’s functions.php are globally accessible, and the events API intends that we call functions when we want something to happen in a template.

So the third way of going about getting a variable into a theme template uses the WordPress events API and uses a function’s return parameter to inject your variable:

In most use cases, by now, you may as well just do what you want to with the variable inside the function anyway, since your function is in global scope, and dispense with trying to inject it into a template:

This is how WordPress was intended to work in a world without any concept of object oriented solutions. “Variables? We don’t need no stinkin’ variables.”

It’s the introduction of an object oriented approach that makes the passing of objects rather than functions critical – because you need to get at the object from within your template in order to access it’s methods/behavior/functionality. And the only way to get at the object is to pass it in as a variable or instantiate it within the template. Instantiating within the template, most of the time, is probably not what you want, because (objects being stateful) you already have an object ready to go, and you don’t want to create a new one; you want the one that’s in the state to do what it needs to.

So with an object, you’re looking to do something in a template like:

$theme_object->do_stuff_with_what_i_already_set_up_in_this_object();

The question is, how are you going to get $theme_object into the template? And probably the easiest (least hack-y) way is to grab it by some pre-arranged hook and filter as in the example above. At the point you have hooked your object to a filter in functions.php, you can apply it in the template and then access it’s functionality:

A recent article on the WP Tavern confirmed a hunch I’ve had for a while now. Creating a few products (WordPress themes and plugins) might be a better way to go.

Duh! I worked for a year for a company (AwesomeMotive – and yes, they are awesome) that sells plugins. You’d think I’d have picked up that idea long ago.

The theme market is notoriously hard to make a living in. There are thousands; and to hear the talk, most of them fail to turn a profit. I have this Arras theme. I could maybe build a couple more. Building themes is what I do for clients now.

I’d guess that themes are hard to sell on the open market because the presentation of a site is so subjective. It really comes down to tastes and trends. The themes that sell well do so because they also offer some kind of site behavior – functionality – that customers want. Which is problematic, because buying your functionality along with your presentation locks them together. When the trends and tastes change, you can’t change without losing the “thing that I bought this for” which is to take reservations for your restaurant (or whatever your real need is).

Implied in this scenario is that what most “theme customers” are really looking for is the functionality: the reservations or the real estate listings, or whatever it is that makes their business go. That’s what plugins are for. And the functionality isn’t a matter of taste and trend. It either does what you need or it doesn’t. And if you need it to make your site work, well, paying a fair price for getting what you need to do business is an investment most people would make.

I got the itch to start fooling around with my old Arras theme project again the other day.

After looking at what I’d started on earlier this year, I decided that rather than messing around with all that legacy code, it would be easier to start from scratch.

With a clean slate I figured I could build the whole thing, not just to be mobile-compatible, but mobile first, using CSS grid. And I can write it to maximize performance with PHP 7.

I’m beginning with the original (“legacy”) Arras look. From there, if time and inclination allow, I’ll build out the default look of the 1.4 version, and then the 2.x version. (The 3.x versions I did a couple years ago went back to the version 1 look.)

After a short stint of coding I have a not-even-bare-bones skeleton. Some basic back-end groundwork is laid. Enough to get a blog page up on the screen, but without header, footer or menu.

or, “The Essay Your Teacher Never Assigned for the Last Day of School.

There’s a t-shirt I’d like to get for Christmas. It says, “I’m a web developer. I solve problems you didn’t know you had in ways you can’t understand.”

I spent a lot of time over the last month working on a little project I inherited last year called Arras.

Arras is a WordPress theme.

Already, I should back up. WordPress is software that runs websites. As of 2015, something like 28% of websites are built using WordPress.

All those sites, even though they’re built using the same basic software, look different. That’s because when you build a website with WordPress, you get to pick your own theme. Like when you go to the ice cream stand, it’s all ice cream. Once you have your basic ice cream, then you add the unique combination of ingredients that make rum raisin different from fudge praelene crunch. Those extra ingredients that give the website its “flavor” make up the theme.

So Arras, a WordPress theme, is a package of code ingredients.

It’s a very old (in internet years) package of ingredients. It was originally designed by a guy named Melvin Lee, but in 2012 he gave up working on it. Which was fine. It had probably run it’s course.

Except that one of my clients was using Arras as the theme for her website. The theme started doing funny things and the site didn’t look right. So I started fixing it bit by bit.

In hindsight, I should have recommended she just swap out the Arras theme for something more up-to-date. That would have been the smart thing to do. But instead, I did something rash. I picked up the theme as a project and started making the fixes available to everyone and anyone.

It turned out to be a much bigger project than I had bargained for. The more I fixed, the more fixes needed fixing. The rabbit hole went pretty deep. And then people started asking for new features.

That’s the story. Last Friday I published a revised and updated version. It’s the culmination of 18 months of fixing — and it’s still not really done. Software is the sort of thing that never really gets done. There’s always something to update, something to fix or something to add.

So 18 months, and it’s still not perfect. But, hey, it works. And I get to say, “I shipped that.” Which feels good.