I know I have not been updating this blog as actively as I should, obviously an excuse would be “I have been rather busy”, which is almost an universal excuse, another reason for it was because ZAYBlog loads so slowly.
How slow? If in case you have been to ZAYBlog for the past two days, loading the index page, on average, took almost a minute.
So, I was looking into optimizing this little blog and after hours of Googling and seeking advice, it basically comes down to a few points:
Get rid of as many of function calls as you can in the themes
WordPress is designed to work with any websites and could be easily configured. Unfortunately a side-effect of this kind of extraordinary flexibility is that everything is pulled from configuration items and from the database. An example would be this:
<div id=”logo”><a href=”<?php bloginfo(‘url’); ?>/”><img src=”<?php echo get_option(‘simplo_logo_img’); ?/>” alt=”< ?php echo get_option(‘simplo_logo_alt’); ?>” /></a> < ?php echo get_settings(‘blogdescription’);?></div>
As you can see, there are FOUR php function calls just to get the logo on the top page! Is this really necessary since we hardly update the header nor the logo? Obviously not.
So how about change it to something like this:
</p><pre><div id=”logo”><a href=”http://www.zayblog.com/”><img alt=”ZAY Blog” src=”Logo.png”></a> Blog of ZAY</div>
As you can see, no PHP function call is necessary and unless you need to change the file name of your logo or change your website name (which I have not been changing for years), there is hardly any needs to update this.
Another example is here:
<title>< ?php wp_title(‘«’, true, ‘right’); ?> < ?php bloginfo(‘name’); ?></title>
Which we can obviously change it to something like:
<title>Wonderful Blog of ZAY</title>
And so on.
Remember, a lot of these PHP function calls needs to make query to the database, which takes time, avoid them at all cost.
NOTE: There seems to be an issue with W3 Total Cache and the hard coded title and headers, I will discuss that in more detail in another post.
Get a CDN
What is CDN? CDN stand for Content Delivery Network, which, in the most general term, is to cache the static contents in your websites to their servers around the word so that your website will load quickly. If you prefer free stuff, Cloudflare would be an ideal option as it is free and is also served as both a DDOS and privacy protection layer and a DNS service, on top of being a CDN, since essentially the real IP of your server is hidden behind the their powerful servers.
If you have a bit more money to spare, then MaxCDN has provided a great paid option. For ZAY Blog that you are reading right now, thanks to W3 Total Cache, I enabled both MacCDN as well as Cloudflare and it definitely speed up this blog quite a bit.
Delete all the junks
After running this blog for more than four years now, I have tried and tested at least 50 plug-ins to make it better. However, some of the plugins did not turn out to be suitable for my blog and I just removed them.
So once you hit delete, you would think everything is gone, correct? Wrong, there are still files hanging around as a result of the removals. Therefore, it is up to you to go in to wp-content and remove the stubborn files that just refused to go away.
Sometimes the plugins also create database tables, and were not deleted when the plugins are uninstalled. In order to fix this issue, you can either go into MySQL yourself, or, if you are not a database guru, run this plugin might be able to help you somewhat (Disclaimer: I have not used it and therefore not sure how effective it is).
Obviously, along the way you might have tried to deploy some themes as well, remember to remove them if you are no longer need them.
Just remember: If you are not using something, then it is junk, and if it junk, you get rid of it from your WordPress installation as soon as possible.
So I looked at the code, removed the junks and set up the CDN, the blog still took a long time to load and the CPU indicator on the CPanel goes directly to the red zone whenever I hit on refresh. What happened? I could not figure out why so I had to use my ultimate weapon: reset.
Basically, I deactivated all the plugins and set the theme back to the default Twenty-Eleven theme. Then, I first switched back to the current theme, hit on refresh, CPU usage stayed low.
Good, then I enabled the plugins, one at a time, and hit refresh on my browser every time I enabled a plugin and check the CPU usage on CPanel every time I load the website as well as note the time that the blog is loaded. For the first 15 or so plugins, the blog continue to load in pretty fast and there was no spike in the CPU usage. However, once I enabled PHP Speedy, which is no longer listed on the official WordPress plugins directory, the CPU usage shoot to 100% when the website was loading. Without any doubt I quickly disabled the plug in and removed it and kept on testing (since I was not sure if PHP Speedy was the only one which caused the issue) and the website loaded relatively fast without too many issues.
Yes, this is a tedious process but it ended up helping me resolve my issue, and I was really happy when ZAY Blog is loading fast again!
There are only two ways for a website to load fast: either you slim it down as much as possible by removing both unwanted/un-used code and plugins or you get rid of the buggy ones that slows down your code. CDN will definitely help to cache the request and therefore speed up the loading process as well. If you have a website getting millions of hits a day, having 100% CPU usage is not impossible and you should really consider moving to dedicated/VPS hosting. However if you blog is like what I have, then it really should not need that much juice to keep the website showing up in front of your readers.