Don’t Repeat Yourself is the rule, the name of the game, and what you should apply to just about everything you code. It is a simple principle which seems to be totally ignored when it comes to WordPress themes. So what is DRY? In it’s simplest form it means don’t write the same code over and over again.
WordPress themes seem prone to this sort of repeating your code. I believe this mostly has to do with WordPress theme developers not being “developers” in a traditional sense, more designers who can code. This habit of repeating code also comes from introductory tutorials and the like which have you creating templates which are mostly the same thing over and over.
Themes are getting more and more advanced, WordPress itself is getting more flexible. As these themes get more complex they need to be simpler. I learned this with my Checkmate theme. It was quickly getting out of hand with the numerous files, lack of organization, and changing code took entirely too long.
K.I.S.S.
I want to rock and roll all night, and party every day too, but I’m busy changing one line of code…in ten different template files. That shouldn’t happen, my new goal is now to create the lightest template files I can, and be heavy on other tools. I also like to cut down on the number of templates I have. The simpler the file structure and code the easier to change or update.
HTML and CSS strive to separate style from content, so you should also try to separate logic from your views. Essentially separate your php from your html as much as possible. This is much more apparent on really complex themes requiring more than your basic template tags.
The Tools
So what tools are there to help in the endeavor to not repeat ourselves? There are quite a few stupidly useful ones.
Template Hierarchy
The first step is understanding what files WordPress looks for at any given time. This can help you strategically use files and save yourself a lot of copying and pasting. The best place to learn about hierarchy is the WordPress Codex Template Hierarchy page.
A quick example of how I use the hierarchy is by using archive.php to display category, tag and author pages. Often these pages are the exact same with minimal changes. Using conditional statements you can serve parts of the page depending on what is requested. Checkmate’s archive file uses this type of system to dynamically display information for tags, categories, or author information.
Conditional Statements
After you know the template hierarchy, you’ll need to understand the use of conditional statements to make the most of your template files. I’ve written twice about conditional statements: Write Less Code, and Complex WordPress Conditional Tags. The WordPress codex page on Conditional Tags will explain everything in great detail.
Conditional tags are just ways to create powerful PHP if else statements very easily. You can use them to dynamically display select parts of a template only in certain specific circumstances.
The Functions File
The functions file is your friend, a powerful friend, a good friend, a wing man of sorts. I use the functions file to call all my other goodies, allowing me to keep a nice clean structured theme folder. Pearsonified has a great article on how to use the functions file. Toxane also has a good article with some examples for WordPress’ function file. The function file can be used
PHP Itself
Some basic PHP can go a long way coupled with some of WordPress’ functions. Learning to read the API and knowing the lingo can go a long way in accomplishing whatever you want.
Helper Functions
In the near future I’ll be releasing my WordPress Tools Project, which is a bunch of functions all ready to be included into themes to help do some mundane things. But you don’t have to wait for me, make up your own, go back into old themes and abstract out some of the advanced things you do. Always using the same HTML code around a template tag? Put it into a function and make your own template tag.
I’ve started posting some of these functions to serve as examples: WordPress Navigation Helper, and Dynamic Multi-Level Page Menus.
Some Tips
Try to keep things organized, for instance put all your javascript in one folder, all your php functions in another, css in another.
Develop a solid framework. I find myself working off Checkmate a lot, I know it very well and know what can be stripped out immediately. Developing a solid theme and releasing is a great way to find errors in your code, essentially it is like having thousands of testers in all sorts of different scenarios.
Learn from other themes. There are a few themes that do this extremely well. Thematic is one of my favorites.
2 Comments
Leave a CommentGood tips…you’ve got a small typo on the h tag for template hierarchy here, causing the text to jumble up.
<h4>Template Hierarchy</h3>
Mismatch on the closing tag (should be h4).
18th Dec 2009
Thanks Adam, it’s fixed. Wish Firefox didn’t fix things like that cause I never even notice.
21st Dec 2009