WordPress header.php Optimization Tips

WordPress is very fast when you first install it, but there are many things you can do to optimize your WordPress site for more speed.

In this article I will outline a few tips you can do use improve your header.php file.

Beware of Theme Updates

If you implement some or all of these suggestions, you will loose them if you do an automated theme upgrade in the future.

WordPress header.php file

The header.php in your WordPress theme gets called every time a page loads on your website. Any optimization that can be made here will affect all pageloads on your site.

WordPress theme developers have to develop the theme to be used on a wide range of WordPress websites, but once installed and configured, you can make some changes to the header.php file to make your site run a lot faster!

1. Hardcode HTML constants

Here is how the top of my header.php file looked. I’m sure yours will look somewhat similar.

4. Multiple calls to bloginfo()

Throughout your header.php file you will see several calls to bloginfo() and some parameter. The function returns important information about your WordPress site, such as name and url location of your theme.

The WordPress function, bloginfo(), fetches and returns the data from the database.

If your theme depends on the exact same call more than once, you can improve the speed.

<?php bloginfo('template_directory'); ?>

You will most likely find this piece of PHP code called several times throughout your header.php file.

We can save processing time by storing the value in a PHP variable, and then simply output that variable instead where needed.

Somewhere before the first time the call is made, insert the following PHP code:

<?php $template_directory = get_bloginfo('template_directory'); ?>

The get_bloginfo() is very similar, except than instead of simply outputting the value, it returns it to the variable instead.

From now on we can simply replace each occurrence of

<?php bloginfo('template_directory'); ?>

with

<?php echo $template_directory; ?>

Repeat the process with any more calls you find repeated several times, each time saving a bit of time.