WordPress Core updates are considered so solid these days that most hosts auto-update WordPress. It’s a different story for themes and plugins, which are often the culprit when a WordPress site starts misbehaving after an update. At WerkPress we specialize in customizing WordPress themes and plugins, but we also do a fair share of *fixing* code. Put simply, there some rather poorly written themes and plugins out there; as WordPress evangelists it’s our mission to raise the bar. Here are some best practices (and common mistakes to avoid) that will help build better themes, make clients happier and reduce support requests.
Enqueueing Scripts, CSS, & Libraries
Time and again we see HTML references to Javascript and CSS files. This can cause the same libraries to be loaded over and over, creating errors. Since version 2.6 WordPress has built-in methods to deal with multiple dependancies on libraries, using wp_enqueue_script() and wp_enqueue_style() can prevent these collisions. I’ve heard this advice repeated hundreds of times on other blogs, yet many still put them in the HTML.
Mo Libraries Mo Problems
More and more libraries of code are being included in themes and plugins, and while they are awesome (jQuery in particular), they can bring problems. For example, jQuery 1.9.0 was released on January 15,2013 and depreciated the live() function for the newer (and better) on() function. It was common practice to use the live() function to bind click events before on() was available. Unfortunately, many theme and plugin authors have failed to update. We’ve already received quite a few requests to fix this issue. So how can we deal with external libraries being updated when we don’t have control of this third party code?
- Link to a fixed, not latest, version on CDNs. It’s considered best practice to link to a CDN hosted version of a library. Then, if you’ve visited another site that uses the same file, it will already be cached in your browser and you’ll experience faster load times. But often we see authors pointing to the “latest” versions on CDNs. So when this library is updated, the theme will point to the new file and potentially break. If you want to ensure your site won’t break every time the latest and greatest libraries are released, instead point to a known supported version.
- Use the bundled libraries in WP Core. While you lose the advantage of a CDN, WordPress bundles many common libraries-like jQuery- right into core. This makes it easy use an updated version while keeping an eye on WordPress release cycles, which can be tested daily using the WordPress Beta Tester plugin and give you a heads-up to test your theme when libraries are updated.
WordPress Core
In every WordPress update some old, replaced, or unnecessary functions are moved to a depreciated file for safekeeping and backwards compatibility. If possible, authors should regularly remove/replace depreciated functions from their themes and plugins. Why? Some argue that this is the beauty of WordPress; it worked in 2008 and it works now. But by using old functions, you’re missing out on performance increases and new usability afforded by these new functions. Also, at some point in the future, these depreciated functions may be removed entirely. Here are a few ways to track down and clean up those pesky depreciated functions:
- Use WP_DEBUG(true); This is a best practice when developing in WordPress. Used in combination with a few other settings, you can log everything to a file and avoid nasty looking errors in your beautiful themes. Our preferred settings at WerkPress are: define(‘WP_DEBUG’, true);
define(‘WP_DEBUG_LOG’, true);
define(‘WP_DEBUG_DISPLAY’, false); This will log all errors the file /wp-content/debug.log. - Use the plugin Log Depreciated Functions. This neat plugin provides a automated search of your WordPress Themes and Plugins for depreciated functions that should be replaced. It puts them in a nice GUI interface and even suggests the replacement function.
PHP Depreciations
PHP has been releasing new versions at a good clip. Just like WordPress, the creators of PHP deprecate functions, but in this case they often don’t maintain backwards compatibility. This can be risky, as with most cheap shared hosting companies, you don’t have much choice when it comes to PHP versions. One day a large host could do a collective update and break thousands of sites that use depreciated functions. While probably a rare occurrence, it still good to periodically review latest versions of PHP what they are depreciating.
Child Themes
If you’re looking to customize your theme and your author provides regular theme updates, child themes are the way to go. A child theme will separate all of your hard work from being erased when the original theme gets updated. It accomplishes this by separating your code from the parent theme in a discrete folder that uses the parent theme as a template. This allows you to override templates, functions, and styles from the parent theme without ever modifying the parent theme’s code.
Occasionally, child themes won’t be enough, and you may need to edit the parent theme when making major changes in functionality or you discover parts of the parent theme that are not “overridable”. In this case, you’ll want to make note that you are no longer able to update. At Werkpress, if we must edit the parent theme we’ll usually remove the update capability to prevent an accidental update and loss of code.
This is the End
We hope this rundown has been helpful to a fellow WordPresser or two. Don’t forget, next time you see a mistake like any of the above – speak up! It’s in the spirit of WordPress to help the community grow by writing the best code possible. Now let’s raise the bar and build some awesome WordPress stuff.