I work with new WordPress users and developers daily . Often times, these users are frustrated with WordPress, or what they think are WordPress problems. Generally speaking their frustrations are not with WordPress per se, it’s often with the theme they are using. Sometimes it’s a commercial theme, and sometimes it’s custom-created by a developer. In either case, they have hit a wall and usually, this can be traced to poor development practices.
Code For The Future and for Others!
You must always assume your code will be touched by another developer in the future! You don’t want that developer (or your future self) cursing your name. If you have written shoddy code without following best practices, you’re making a life for the next developer pretty hard. The following are specific tips to develop with consideration and elegance.
Comment your code well
This is something you hear time and time again, but it’s worth mentioning again. Documenting your code will help not only other developers understand what your thought process was when writing that spiffy function, but it will also help future-you when you need to tweak something in 8 months. It’s easy to forget what you were thinking at the time the code was written or why you did something a certain way. Your future self will thank you for your present self’s clear documentation.
Use coding standards
Enqueue all the things
Become a better coder
Almost every developer, including the really, really good ones, will tell you they look at the code they wrote last year, or maybe even 6 months ago to see how they could make it better based on what they have learned since. No one is born an advanced coder. Developers need to be open to change, feedback, and learning.
Try to get your code reviewed by your peers if you can, or discuss with other developers their approach to solving coding problems.
Meetups are a great place to meet other developers. Find a Meetup or a WordCamp in your area. If you don’t have access to a local meetup, there are great online communities such as the Advanced WordPress Group on Facebook.
Read the Experts
Follow the blogs of people like Bill Erickson, Mark Jaquith, Justin Tadlock, Tom McFarlin, Pippin Williamson and Paul Underwood to help cut your learning curve down and stay on top of best WordPress coding practices.
Use the Theme Check and the Theme Mentor plugins to improve your code. You can use both plugins simultaneously since they offer slightly different results. They will scan your theme code and show you a list of errors, problems or recommendations, based on what the WordPress Theme Reviewers team looks at in themes submitted for the repo.
Don’t Reinvent The Wheel – There’s a WordPress API For That
WordPress has a number of APIs which can make your life easier *and* help make your theme or plugin future-proof, extendable and just, you know, better. Before you go trying to invent things from scratch, see if WordPress provides specialized functions you can tap into first. The WordPress API’s often have characteristics like data sanitization built in, making your code more secure by default, which is a good thing. Familiarize yourself with the Codex page to discover time and code saving API’s.
The Power of the Plugin
WordPress’ system of hooks, actions, and filters will get you far. They are listed as part of the *Plugin* API, but even if you are developing a custom theme you will be able to make use of them.
They allow you to modify all kinds of things about WordPress functionality without actually hacking WordPress core files (a huge no-no). Jeffrey Zinn has a great intro presentation on how to get started with these:
Once you understand these concepts, a whole new world of possibilities opens up to you.
Consider: Theme vs Plugin
When building a custom theme, whether for a client or for commercial / general release, it’s important to consider whether some of the functionality you want to include should be included in the theme, or incorporated as a plugin. The most common examples of features found in themes that would serve their function and the site better as plugins are custom post types and shortcodes. There’s a whole debate around this and I recommend you get caught up on the ins and outs by reading Justin Tadlock’s recent post on the topic:
Keep it Lean
I’ve seen many a theme suffer from enormous bloat because they include literally 5 different slider options in the theme! Now while you might think this is great for the end user since they have so much to choose from, it’s not. This is an overly complicated admin experience, creates more possibility for conflicts with other plugins and makes it nearly impossible for future developers to maintain these options.
A good rule of thumb for keeping your theme lean is to stick to the “Recommended or Required Plugins” approach. This is where, upon installation, the user sees a notification letting them know which plugins should also be added. The user can then choose which to turn on or off. Those features then become portable should the user decide to change themes. One way of implementing this is using the TGM Plugin Activation library.
These tips are just scratching the surface of best practices, but hopefully, you are convinced it’s worth your time. The effort you take as a developer to implement these has a real-world impact on the user’s experience with WordPress, and therefore its continued sustainability.