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.

<html xmlns="https://www.w3.org/1999/xhtml" <?php language_attributes(); ?>
<head profile="https://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />

Notice the 3 pieces of PHP code? These pieces of information is collected via WordPress PHP functions that in return uses internal function and database calls.

Does that description sound slow? It’s not that bad, but in terms of optimization, every millisecond gained matters.

The fix is simple.

Load your website in your browser. Find and copy-paste the corresponding code in the source code.

This is the correct HTML code which you can simply copy-paste and overwrite the corresponding area in your header.php

Here is mine after copy pasting:

<html xmlns="https://www.w3.org/1999/xhtml" dir="ltr" lang="en-US" xmlns:og="https://opengraphprotocol.org/schema/">
<head profile="https://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

2. Hardcoded Feed Urls

Unless you intend to change your url feed at some point in the future, you could also save some speed on hardcoding your feed urls.

<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="https://www.concept-i.dk/feed/rss" />
<link rel="alternate" type="application/atom+xml" title="<?php bloginfo('name'); ?> Atom Feed" href="<?php bloginfo('atom_url'); ?>" />

That is 4 unnecessary pieces of PHP.

Here is what mine would be once optimized using the same procedure of visiting the website and copy-pasting the processed HTML code.

<link rel="alternate" type="application/rss+xml" title="CleverWP.com RSS Feed" href="https://cleverwp.com/feed/rss2" />
<link rel="alternate" type="application/atom+xml" title="CleverWP.com Atom Feed" href="https://cleverwp.com/feed/atom" />

3. EXTRA RSS tip – Feedburner

I take RSS optimization one step further by using Feedburner.

I recommend using Feedburner or similar service to speed up your website even more.

  • Less pageloads on your site.
  • Your feed is constantly updated by Google
  • Google visits your site more often
  • Increasing your feed subscriber count can affect your authority and hence rankings
  • Works in all formats
  • Feed reader statistics

Here is what the code is reduced to, and there is no PHP code.

<link rel="alternate" type="application/rss+xml" title="CleverWP.com Feed" href="https://feeds.feedburner.com/cleverwp">

Read more on the Feedburner service page. Best of all? It’s free!

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'); ?>


<?php echo $template_directory; ?>

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

There are many more things to do when optimizing header.php, and with these steps you are well on the way to a faster and smoother running WordPress site.

Cleverplugins newsletter

Never miss a story or discount - sign up now

Cleverplugins Newsletter

Never miss a story or discounts - sign up now