A regular task in my work is to improve the speed of a website. There are many places to tweak, but a place I always go to look for some easy improvements is the header.php file.
First thing to do is to make sure the theme is a child theme to ensure your changes are not overwritten when a theme is updated.
By the way, I recommend checking out the homepage of the theme if you don’t know the author or the WordPress theme. I some times encounter themes that are no longer maintained and its necessary you counsel your customer in this case.
Depending on how they use the website and how complex it is, it can be worth starting to plan a theme change. It’s always better to build on top of an active and well supported theme than an old abandoned theme that still uses deprecated functions or bad code.
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 broad 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 the HTML constants
Here is how the top of my header.php file looked. I’m sure yours will look somewhat similar.
Notice the four pieces of PHP code? These pieces of information are 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 regarding optimizing, every millisecond gained matters. Once you start on this practice
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:
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.
That is four useless 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.
3. 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.
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 just output that variable instead where needed.
Somewhere before the first time the call is made, insert the following PHP code:
The get_bloginfo() is very similar, except rather than just outputting the value, it returns it to the variable instead.
From now on we can simply replace each PHP call with a variable. Faster.
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.