logo

Optimizing Your WordPress Site Pt. 2

logo

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.

Code Modifications

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:

  1. <title><?php if (is_home () )
  2. {
  3. bloginfo(‘name’);
  4. }
  5.  
  6. elseif ( is_category() )
  7. {
  8. single_cat_title(); echo ‘ – ‘ ;
  9. bloginfo(‘name’);
  10. }
  11.  
  12. elseif (is_single() )
  13. {
  14. single_post_title();
  15. }
  16.  
  17. elseif (is_page() )
  18. {
  19. bloginfo(‘name’);
  20. echo ‘: ‘;
  21. single_post_title();
  22. }
  23.  
  24. else
  25. {
  26. wp_title(,true);
  27. } ?></title>

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

  1. <title>Planet Label Blog</title>

Short, simple, to the point, and one less dynamic request made to the server.

Pingback URL

Original Code

  1. <link rel="pingback" href="<?php bloginfo(‘pingback_url’); ?>" />

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

  1. <link rel="pingback" href="/xmlrpc.php"/>

RSS and Atom Feeds

Original Code

  1. <link rel="alternate" type="application/rss+xml" title="<?php printf(__(‘%s RSS Feed’, ‘kubrick’), get_bloginfo(‘name’)); ?>
  2. "href="<?php bloginfo(‘rss2_url’); ?>" />
  3. <link rel="alternate" type="application/atom+xml" title="<?php printf(__(‘%s Atom Feed’, ‘kubrick’), get_bloginfo(‘name’)); ?>
  4. "href="<?php bloginfo(‘atom_url’); ?>" />

So much work for the server when it can be handled like this…

Static Code

  1. <link rel="alternate" type="application/rss+xml" title="Planet Label Blog RSS Feed"
  2. href="http://www.planetlabelblog.com/feed/" />
  3. <link rel="alternate" type="application/atom+xml" title="Planet Label Blog Atom Feed"
  4. href="http://www.planetlabelblog.com/feed/atom/" />

Language Attributes

Original Code

  1. <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>

Static Code

  1. <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">

Content-Type Attributes

Original Code

  1. <meta http-equiv="Content-Type" content="<?php bloginfo(‘html_type’); ?>; charset=<?php bloginfo(‘charset’); ?>" />

Static Code

  1. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

JavaScript

Original Code

  1. <script type="text/javascript" src="<?php bloginfo(‘template_url’); ?>/script.js"></script>

Static Code

  1. <script type="text/javascript" src="/wp-content/themes/PlanetLabel2/script.js"></script>

Stylesheets

Original Code

  1. <link rel="stylesheet" href="<?php bloginfo(‘stylesheet_url’); ?>" type="text/css" media="screen" />

Static Code

  1. <link rel="stylesheet" href="/wp-content/themes/PlanetLabel2/style.css" type="text/css" media="screen" />

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. icon wink Optimizing Your WordPress Site Pt. 2

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. icon biggrin Optimizing Your WordPress Site Pt. 2

Leave a Reply

logo
logo
Powered by Wordpress. Copyright 2009 Brett Widmann.