This is the second part of my optimization series for WordPress sites which focuses on what administrators can do in order to speed up load times for the sites they manage. Part 2 focuses exclusively on some of the coding changes I’ve made to a few sites I manage in order to speed up WordPress. Part 1 of the series can be found here.
One of the great things with nearly any web content management system is its use of themes and templates. In order to make themes and templates simple and easy to use without having to change much of the code, PHP functions are used excessively so that the admin doesn’t have to do much coding (in most cases, no coding at all) in order for the template to work with the system it was designed for. With WordPress for example, simple things such as the site’s title and css file locations are dynamically called using PHP. If you plan on keeping your theme for awhile, or have designed a theme exclusive to your site, much of the dynamic PHP calls that can slow down load times can be eliminated entirely, especially with information from a theme’s header.php file.
For all of the code snippets, Planet Label has allowed me to used code snippets from their blog to use for my examples. Below are snippets of the original code and the changes I made so that less dynamic calls are made to the server.
Site Title
Original Code:
That seems like a massive amount of code for just the title of a page. Not that I also added additional formatting to make the code appear clearer and easier to read. While code like this may be necessary for dynamically rendering a site name on the fly for any site that installs it, does your site really need it now that it’s installed and that the theme will likely be used for an extended period of time or rotated seasonally? Let’s give the title a little more of a static flair instead.
Static Code
Short, simple, to the point, and one less dynamic request made to the server.
Pingback URL
Original Code
As you can see, the link for the pingback URL is also called dynamically in themes. Not sure what a pingback is? Check out the the WordPress Codex.
Static Code
RSS and Atom Feeds
Original Code
So much work for the server when it can be handled like this…
Static Code
Language Attributes
Original Code
Static Code
Content-Type Attributes
Original Code
Static Code
JavaScript
Original Code
Static Code
Stylesheets
Original Code
Static Code
You’ll notice in some of the examples that making the code static causes more code than the original code snippets. Length of code is essentially something we’re not looking into for the results, but rather the fact that less calls to the server are being made. The less the server has to work, the faster your WordPress site can deliver content to your audience. Furthermore, these changes should be made if you intend on keeping your theme for a long time or rotating it with other themes throughout the year. Finally, if you are designing a theme to release for download, it is imperative that you keep the PHP server requests in the header.php file otherwise chances are good that the theme won’t work for the other person trying to use it. Let them do the tweaking and optimizing.
Are there any other code tweaks that you recommend for speeding up a WordPress site? Feel free to drop a comment with your thoughts, opinions and suggestions. The more the better! Happy coding.