Improve Performance of Your WordPress Theme in 5 Minutes

There’s a quick and relatively simple way to increase the performance of your WordPress theme while reducing overall server load. The problem is that most themes are written to be as portable as possible. This comes in handy because you can just drop in a theme and it works with no editing at all. Unfortunately, it means that a lot of information that never changes is queried from the database anyway. If you want to speed your theme up, you can customize it to your site and reduce database queries. All in about 5 minutes.

In order to simplify the post, I’m combining the header.php and footer.php into one file. You should make sure to check both, and maybe even check other files in your theme. Your theme’s header.php and footer.php files probably look something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>

<head profile="http://gmpg.org/xfn/11">
	<title><?php wp_title(''); ?></title>
	<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />

	<?php wp_head(); ?>

	<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>?20090112" type="text/css" media="screen" />
	<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />
	<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
</head>

<body>

<div id="page">

<div id="header">

	<h2><a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a></h2>
	<h3 id="blog-description"><?php bloginfo('description'); ?></h3>

	<ul>
		<li class="page_item <?php echo ( is_home() )? 'current_page_item':''; ?>">
			<a href="<?php bloginfo('url'); ?>">Home</a>
		</li>
		<?php wp_list_pages('title_li=&depth=1'); ?>
	</ul>
	<div id="wpinformer-rss">
		<p><a href="<?php bloginfo('rss2_url'); ?>">Subscribe via RSS</a></p>
	</div>
	<div class="clear"></div>

</div><!-- end header -->
</div><!-- end page -->

<div id="footer-wrap">

	<div id="footer">
		<p>
			<a href="<?php bloginfo('rss2_url'); ?>">Entries (RSS)</a> and
			<a href="<?php bloginfo('comments_rss2_url'); ?>">Comments (RSS)</a>
		</p>
		<p>
			<a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a> is powered by <a href="http://wordpress.org">WordPress</a>.
		</p>

		<div class="clear"></div>
	</div>

</div><!-- end footer-wrap -->
<?php wp_footer(); ?>
</body>
</html>

In the above, you can easily remove language_attributes() on line 2, the bloginfo() calls on lines 6, 10, 11, 12, 21, 22, 26, 31, 42, 43, and 46. In order to find out what to replace them with, simply visit your site and view the (X)HTML source. Find the sections in the code where the functions you want to replace are, and copy the content to your theme. The above files change to this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">

<head profile="http://gmpg.org/xfn/11">
	<title><?php wp_title(''); ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

	<?php wp_head(); ?>

	<link rel="stylesheet" href="/wp-content/themes/wpinformer/style.css?20090112" type="text/css" media="screen" />
	<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="/feed/" />
	<link rel="pingback" href="http://wpinformer.com/xmlrpc.php" />
</head>

<body>

<div id="page">

<div id="header">

	<h2><a href="http://wpinformer.com/">WordPress <span>Informer</span></a></h2>
	<h3 id="blog-description">WordPress News and Resources</h3>

	<ul>
		<li class="page_item <?php echo ( is_home() )? 'current_page_item':''; ?>">
			<a href="http://wpinformer.com/">Home</a>
		</li>
		<?php wp_list_pages('title_li=&depth=1'); ?>
	</ul>
	<div id="wpinformer-rss">
		<p><a href="/feed/">Subscribe via RSS</a></p>
	</div>
	<div class="clear"></div>

</div><!-- end header -->
</div><!-- end page -->

<div id="footer-wrap">

	<div id="footer">
		<p>
			<a href="http://wpinformer.com/feed/">Entries (RSS)</a> and
			<a href="http://wpinformer.com/comments/feed/">Comments (RSS)</a>
		</p>
		<p>
			<a href="http://wpinformer.com">WordPress Informer</a> is powered by <a href="http://wordpress.org">WordPress</a>.
		</p>

		<div class="clear"></div>
	</div>

</div><!-- end footer-wrap -->
<?php wp_footer(); ?>
</body>
</html>

As you can see, all the functions that return static content have been replaced. What did this gain? I went from 29 queries taking 1.391 seconds to 24 queries taking 0.868 seconds. Taking five minutes to fix my theme has eliminated five queries from every page load of my site, and sped up the home page query time by roughly 35-40%. It’s a quick easy fix with a huge return.

Related Posts:

About AaronCampbell
Aaron is the man behind the curtain here at Xavisys. He gets things done and really knows his way around the code.

Comments

6 Responses to “Improve Performance of Your WordPress Theme in 5 Minutes”
  1. You checked that times only once, didn’t you? Because that difference in times is quite nonsense (it’s true you get 3 queries less, but hardly half a second). I recommend the opposite: not to touch that functions and stay dynamic.

  2. The times were actually checked 10x each and an average taken. What you see are the averages when no query caching is active on this very site (home page).

  3. @Aaron D. Campbell – So that is very interesting. I tried to completely remove header from kubrick on localhost and difference in time was practically none. Are you sure the SuperCache plugin was deactivated?

  4. I have 2 blogs on wordpress. these ideas are neat…

  5. Yes, otherwise it would have cached the HTML comments that the times were in and showed the same times.

    Every server config is different, and I was definitely testing in a “best case scenario” manner, but the savings are definitely there. Much of the savings can be gotten by using MySQL query caching too though, so again…Your Mileage May Vary.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!