5 things to know before starting to design a WordPress theme

Posted on September 22, 2010
by

With hindsight, there are things that are definitively not obvious to me when I started to design my first WordPress theme, things which are not very well explained in the official documentation. To begin, be aware that WordPress theming is everything but beautiful coding: no OOP, no MVC, no respect of coding good practices, nor even of common sense sometimes… I’ve learnt these things at my expense, so I’m now going to warn you from the beginning so that you don’t get the cooling effect that I got.

#1 You can forget about MVC

If there is something that totally had a cooling effect on me, it’s how WordPress is coded… When you use it and you never need to write something specific, never mind how it’s coded under. But when you have to get your hands on it, be ready to forget what is a beautiful code: it’s pure procedural style. A get_header() function for the first part of your page, a get_footer() for the last, and in the middle you have the content of your page.

#2 Functions have meaningless names

I know I’m a little picky when it comes to coding. I LOVE beautiful code and I have a profound respect for good practices. But honest, can you guess what does the “the_title()” function do? Does it write the title of a post, does it return it as a string, or something else?.. Well actually the good answer is it writes the title of a post.

But then why the fuck does the “get_search_form()” also writes the search form instead of returning it? What’s your definition of the verb “get”, WP guys?

Ok I’m picky? Here is another exemple to show you how inconsistent and illogical it is. Would you guess the meaning of the “the_post()” function? No, it doesn’t write the body of the post just like “the_title()” writes the title. No! Too easy! Little player… What it really does is, according to the PHPdoc: “Iterate the post index in the loop.“. It’s full of meaning, isn’t it?

#3 Most of the functions don’t return anything, they write content

In your templates, you will use WP built-in fonctions to display your data. the_title() writes the title of a post, the_content() writes the body, the_tags() writes the tags, and so on … Unfortunately it writes those data, instead of returning them. Honest, what’s the most difficult piece of code to write:

<?= the_title() ?>

or

<?php the_title() ?>

Answer: none of them. So why do you echo content in your functions instead of returning their values, and don’t let the “view” (oh sorry, no MVC with WordPress…) I mean let the theme decide to write it right away or to do extra processing (such as adding some tags, attributes, CSS classes,  … or anything!)

Be ready to have fun with ob_* functions sometimes 🙂

#4 There is no template for comments

There are templates for almost everything in WordPress: a template for the index page, for a single post, for search results, for tag pages… But no template for comments? Uh I don’t get the point there… There is a work around technique though, that uses a callback function to write the comment HTML, but honest it’s dirty.

#5 You can’t get the number of posts easily

Maybe there is a technical good reason to that, but honest it was a pain when I was working on it: when you display many posts on your page, you will have to use a While loop, no way otherwise.

Alright, whatever the loop as long as it does the job, will you say? It does the job yes, until you need to know the total number of posts on your page, or you want to deal with the previous/next post, or want to do something special with the last post, like applying a special CSS style just to it.

For and Foreach loops are not monstrous you know WP guys, we’re not afraid of using them ^^ And it gives much more information, such as the cardinality of the posts in advance.

Hopefully you can still trick to get those information, by using SQL queries and so on, as it’s explained in many forums … But again, it’s quite a dirty way to do it.

Ok WordPress is dirty, then what?

Then it hurts my feelings! Because I can’t stop using it because WordPress rocks for other reasons. I was just surprised that WordPress, the leader for blogs in the world, was technically that bad thought. But hey, that’s the game and I did my WP theme anyway because I needed it… Now it’s done I hope I won’t have to get my hands on it too soon.

About the author

Cyril Mazur is a serial web entrepreneur with experience in various fields: online dating, forex & finance, blogging, online advertising... who enjoys building things that people like to use.

15 comments