WordPress is slightly addictive. I always thought of it as a gateway drug into the world of web development.
Maybe you started off just needing a simple website. You figured out how to install WordPress on your server, chose a theme that suited most of your needs, set up some widgets, maybe installed some plugins, and that was it for the beginning.
Then came the day you really wanted something on your page but couldn’t find a plugin for it, and it wasn’t part of the theme options either. Or maybe someone told you about Firebug and showed you how to change the color for an element on your website and it completely blew your mind.
So you became a little more curious.
An intense googling session later, you managed to change some lines of code in the stylesheet of your small, innocent website. As FileZilla pushed back the modified file onto your server — you waited, biting your nails — fearing that your website would be irreversibly ruined. Maybe you would even break the internet.
But lo and behold, instead the site did exactly what was promised by that anonymous forum post.
That’s when you became hooked.
What followed was a blur of setting up your first child theme, changing the header image without using the theme options, setting up post templates, pasting some PHP code in your functions file, registering a new widget area, and wondering when exactly you became fluent in css.
As the dust settled and you leaned back in your chair, you realized that by now the site had nothing to do with the original setup anymore. The color was changed, all the headings were customized with new typography, and you even got rid of that secondary navigation that always bothered you.
By the love of God. . . Did I just create my own theme?
You sure did. Even the person who made the original wouldn’t recognize it anymore.
Maybe by now you’re ready to take it to the next level. You’re done merely modifying other people’s work and instead want to go for the big one: your own theme in the WordPress theme directory.
You want to do it right, right? Not just throw it together so that it just barely works? In that case it’s important to become familiar with a few best practices for theme development, so you produce a beautiful work of art and not a crude hack job, even if you don’t want to make your theme available for other people.
Respect WordPress Coding Standards
As anyone who was ever hired to work on a website that was created by someone else can attest, it can be painstaking if the other person has their own special way of writing code. Not only do you have to understand the architecture of the site in general, but you also first need to figure out how to read it.
To avoid this type of situation, WordPress has well-established coding standards for PHP, HTML, and CSS. The standards have been agreed upon by the WordPress community as best practices. Following these standards will make it much easier for other developers to read and modify your code.
The reason why they exist is to create a baseline for collaboration. This is fundamental because (as you know) WordPress is open source, and therefore has contributions from many different sources. Same goes for themes and plugins, which are written by individuals and then made available to everyone else.
The coding standards help avoid common errors, improve readability, and make modifications easier. They also ensure congruity across all files. Following the standards means your code can be read, understood, and modified by anyone regardless of when it was written and by whom.
Example:
To ensure legibility of CSS, WordPress coding standards recommend the following structure:
- Use tabs, not spaces, for indentation.
- Give each selector its own line, ending in either a comma or an opening curly brace. Property-value pairs should each have their own line. The closing brace should be using the same level of indentation as the opening selector.
Correct:
#selector-1, #selector-2, #selector-3 { background: #fff; color: #000; }
Incorrect:
#selector-1, #selector-2, #selector-3 { background: #fff; color: #000; } #selector-1 { background: #fff; color: #000; }
Much better, right? You can find the coding standards in the WordPress Core Handbook:
- WordPress coding standards for HTML
- WordPress standards for CSS code
- PHP coding standards in WordPress
- Javascript code standards for WordPress
Keep Your Theme Files Organized
Every web project is different and requires a different set of resources. Some applications run on desktop computers and manage with a single language. Some need a second one and aim for mobile devices.
Other projects — such as WordPress Themes — can include anywhere from three to four (HTML, CSS, PHP, and JavaScript) languages. Moreover, theme components can run on the side of the client or server, and communicate directly with WordPress or the database.
Due to these complexity concerns, keeping your resources organized is vital. Failing to do so just makes it messier than necessary, and hard for those who want to build upon your work — be it other designers, developers, or users. WordPress suggests certain standards for organizing your PHP template files, stylesheets, JavaScript sources, and images, which are easily followed and can be found in the WordPress Developer Handbook.
Theme file organization in short form:
- Set up template files for individual page components (header.php, footer.php, sidebar.php etc.) instead of cramming it all into one file.
- Keep your main template files within the theme’s root directory.
- Give JavaScript, page templates, CSS, image and language files (more on that below) their own directory.
- Be unambiguous in file naming, prefix page template “page-about-template.php” or nest inside “page-templates” folder.
- List of theme files automatically recognized by WordPress
- Template File Checklist – the files that compose a basic theme and what each should include
Adhere to Template Hierarchy
Since we are talking about file organization: The different components of WordPress fit together like a big puzzle. In order to generate a website, templates and files are used to produce its different parts. Some of them are displayed on every page (think header or footer), others (such as category or search pages) appear only under certain conditions.
There is a hierarchy in place that determines which template the WordPress platform will use for each. If the first choice is not available, WordPress will then use the second on the list and so forth. For theme development, a proper understanding of this hierarchy is vital and you need to familiarize yourself with it to ensure that what you’re developing is using the correct templates in the intended places.
It can be a complex topic. However, in order to make it a little easier, in 2013, WordCamp Minneapolis put together this poster:
Alternatively, there is of course official documentation on template hierarchy in the WordPress Codex, which provides an explanation for how theme files fit together, and how each is rendered by WordPress. ‘Stepping into Templates‘ also provides a detailed breakdown of template files and their structure.
Internationalize/Localize Your Theme
This might come as a shocker, but WordPress is no longer a niche product used by a small group of developers who are all hanging out in the same online newsgroup. It has become a worldwide phenomenon. I know, I’m as surprised as you are.
That also means that not all WordPress users come from English-speaking countries. In fact, two-thirds of users live outside the US.
And because the platform has been so widely adopted, the market for themes isn’t limited to a single language.
On top of that, WordPress makes it incredibly easy to localize your theme, and requires only little extra effort. If that’s not enough reason to make your themes translation ready, I don’t know what is.
Internationalization vs. Theme Localization
Making your theme accessible for non-English speakers comes down to two acronyms: “i18n” and “l10n.” I18n is an abbreviation for internationalization and is called i18n because there are 18 letters between “i” and “n.” Similarly, l10n is the numeronym for localization (as in “L” followed by ten more letters, and then “N”).
Internationalizing your theme means you make it ready for translation. What it basically comes down to is changing the text strings inside the theme into functions so they become translatable. Localization is the actual transfer of theme strings from one language to another. If you are fluent in another language, you might translate your theme yourself. Otherwise you can at least give others the chance to do it for you.
To achieve this, WordPress uses the GNU gettext localization framework. Developers wrap translatable strings in special gettext functions. The source code files are then parsed by special tools and the translatable strings extracted into POT files (which stands for Portable Object Template).
The second step involves translating the text inside the POT file into the target language, saving both the original and the translated strings in a PO (Portable Object) file. PO files are identical to POT files except for their purpose, which is being compiled into binary MO (Machine Object) files in step three. These files provide faster access to the strings for machines.
Resources on this topic:
- Theme Developer Handbook: Internationalization explains how to turn strings into functions.
- The WordPress Polyglots Handbook is a work-in-progress and set to become the main resource on all things concerning i18n and l10n.
Tools for creating POT files:
- Poedit – a free gettext translations editor
- Launchpad – a software collaboration platform which can be used for WordPress theme translation
- Pootle – tool for community localization
- GNU Gettext – the original GNU Translation project
If you want to offer your help localizing WordPress itself and its subprojects, you should swing by GlotPress, the official translation tool for the WordPress project.
Test Your Theme
As with every good piece of software — a WordPress theme being nothing else — a considerable amount of time has to go into validating that everything works as it should. There is nothing more frustrating than installing something that is full of flaws, and nothing will make people stop using your creation quicker.
That being said, testing is a mandatory part of theme development before releasing your work to the public. In addition, if you are planning to put your theme into the WordPress directory, the validation team there will do the same and will reject your theme if it doesn’t measure up. Rejection hurts, so you might as well address errors beforehand.
In case the goal is for your theme to be added to the directory, it must pass the guidelines laid out in the Theme Review section. However, even if you are using the theme for yourself or a client, you should run it through a testing regimen similar to the following:
- Find and fix PHP and WordPress errors. Enable debug settings by adding “define(‘WP_DEBUG’, true);” (without the quotation marks) to your wp-config.php to see deprecated function calls and other WordPress-related errors.
- Check template files against the Template File Checklist to make sure they meet WordPress standards.
- Validate HTML and CSS. WordPress provides an extensive list of code validation tools.
- Check for JavaScript errors if you have used it in your theme.
- Run the Theme Unit Test: Download the WordPress export file from this page, import it into your installation, and follow the instructions for a thorough theme test.
- Test theme with all your target browsers. For example, IE7, IE8, IE9, Safari, Chrome, Opera, and Firefox.
- Test-drive with real people. Ask friends, relatives, and co-workers to try out your theme before making it public.
- Clean up comments, debug settings, and other items that have no place in the final product.
Other Useful Tips
Follow WordPress Development News
Since WordPress is constantly changing, anyone interested in developing with the platform is well-advised to stay up to date on the latest news and announcements related to WordPress development. This way you can make sure you are aware of new features, security fixes, and other aspects of WordPress that might affect your work.
Some helpful sites include:
- The officual WordPress.org blog
- WordPress Planet
- Make WordPress Core Development Blog
- VIP News
- TorqueMag.io (but you already knew that)
General Resources for WordPress Theme Development
- WordPress Theme Development – Massive article about everything related to theme development
- WordPress Site Design and Layout – Step-by-step information on website design with WordPress for beginners and advanced users
Do you have anything to add to this list? Share your thoughts in the comments below!
6 Comments