Using Constants to Streamline Your WordPress Theme

Working with a large corporate site that gets plenty of traffic and runs WordPress has taught me a few things about WordPress that I don’t particularly like.   The biggest complaint I have is that WordPress views the database like a swarm of bees views a bear attacking their hive: hit it repeatedly hoping that it goes down.  Doing a freelance gig and discovering that your client’s hosting has a database cap of 75000 hits/hour is not a fun situation when they’re getting around 1000 page views per hour and it’s pushing that cap.  Caching is great except that this was go-live, and there was a lot of data to cache for the first few hours.  Even still, caching doesn’t help everything.

So what can you do to help smoke those bees down?  Get a little nimble with your code and stop relying on WordPress conventions for everything.

bloginfo() and get_bloginfo()

These two functions are bad news, because they make a database request every time.  If you’re designing a custom theme for a site and you’re not expecting a lot of changes to the name and tagline, making heavy use of these functions is going to bog you down. Here’s what I did:

define('THM_TITLE', 'Page Title');

Then, where you’d be using bloginfo(‘name’), you just change that to echo THM_TITLE;

Instead of relying on get_bloginfo(‘name’) to hit the database just to figure out what my site’s name is, I just tell it in code with a constant.  I used the THM_ prefix to localize it for my theme and ensure I’m not crashing into any other defined constants (in practice I use a more descriptive prefix).  Obviously, hard-coding the name doesn’t work if you’re running a multisite or are building a theme for wide use, but if you’re building for a static installation to use WordPress as a CMS, you’ve just saved yourself one database call.  The name is just the first thing to tackle, though.  The biggest culprit I encountered was get_bloginfo(‘siteurl’);.  I used this all over my theme to make sure my images and stylesheets and script calls were all originating from the correct URL.  Now, it’s technically OK to leave this out, but let’s go for accuracy.  I work with development and integration servers, so this needs to be dynamic.  At the top of my functions.php, I added

define('THM_SITEURL', get_bloginfo('siteurl');

And then I replaced every instance of get_bloginfo(‘siteurl’) with THM_SITEURL.  That saved me 10 database calls on my home page alone.

Here’s a few others that I use:
Theme diretory URL:
define('THM_TEMPLATE_DIRECTORY', THM_SITEURL.'/wp-content/themes/theme_directory');

Pingback URL:
define('THM_PINGBACK_URL', THM_SITEURL.'/xmlrpc.php');

Charset:
define('THM_CHARSET', 'UTF-8');

All told, between those five defined constants, I reduced my database calls on my home page initiated by my theme from 22 to 5.

Don’t stop there, though.  Just about everything in get_bloginfo can be hard-coded for a standard, out-of-the-box WordPress setup.  And chances are, if you’re running a highly custom theme, you can deal with hard-coding the rest.