WordPress is a great blogging platform in part due to the active developer community creating plugins to extend the functionality of the basic application. If you’re like me you’ve added quite a few. Over time all those plugins can seriously slow down your blog, but there’s some relatively simple coding you can do to help shave precious seconds off the download times of your pages.
Solving the Plugin Creep Problem
The fix for Brian’s Threaded Comments plugin was a two part step, which will often be the case with other plugins.
Copy the code and place it in it’s own file. Then replace the code in the original file with a link to the new external file.
Use conditional tags so the external files only load where needed: Moving the code to external files will speed up pages once the files are cached, but they don’t help the first time the file is encountered. That’s perfectly acceptable, but why load all those files before you need them?
The second step in the process is to control when those external files are first downloaded. In the case of the threaded comments plugin the script and css aren’t needed until you land on a page that accepts comments. In my case that’s only the single post pages. So how to only load the file if the page in question is a single post page?
Fortunately WordPress makes it easy through conditional tags, specifically the is_single() tag for this case.
What you’ll do is wrap some simple php that uses the conditional tag around the link to the external file
1 2 3
The above code will test to see if the page in question is a single post and if it is it will call the script. If the page is something other than a post the script won’t be called ensuring the script is not loaded until it’s first needed. It’s similar to conditional comments except here you’re testing for a specific page type within your blog instead of testing for a specific browser or browser version.
There are many other conditional tags such as is_page() and is_category(). You can even test for a specific post or page by adding the id like is_single(97), which would check to see if the page in question is the single post with an id of 97.
With the Lightbox 2 plugin I had to take a similar, but slightly different approach as the page in question isn’t a post or WordPress page. The solution called for a few extra lines of php to test for the current page, though again it was fairly simple.
By moving code to external files and controlling when those files were first downloaded I’ve been able to save about 2/3 of the download time across many pages of the site. The work took less than an hour. Pages that were taking 35+ seconds to download are now down to about 12 seconds. Still slow and I have more work to do in other areas, but a very noticeable reduction in load times as is.
Think about the plugins you’ve installed and ask yourself when the client-side code those plugins use is really necessary. Move the code to external files and keep those files from loading until they’re first needed and you could save significant load times across your site.
While you’re at it give Joost’s post on speeding up WordPress a read for some additional tips on making your blog faster.