Monday, July 2, 2012

2 Simple WordPress Blog Optimization Tips and Tricks

I have been pretty busy with the WordPress plugin development lately, so didn’t get a chance to write any posts for a few weeks. I think it’s about time I share a simple but effective website performance improvement trick with the readers of this site that I have been using on all my sites. If you haven’t read the WordPress site optimization tips and tricks already then you should read that first.
This trick involves changing some of the dynamic wordpress queries to static ones which in turn reduces the number of database queries and improves site performance. This does require a little bit of PHP knowledge but it’s really basic stuff.
In your WordPress theme’s header and footer you will see many statements similar to the following:
bloginfo(‘home’)
This queries WordPress for the home URL of your blog (eg. http://www.your-site.com). Now, how often do you think your home URL is going to change? Most likely never! So why not replace it with the static information which is “http://www.your-site.com” and make one less database query?
Saving one database query may not sound very convincing but you probably have many statements like the following that can also be replaced:
bloginfo(‘html_type’)
bloginfo(‘charset’)
bloginfo(‘name’)
bloginfo(‘version’)
bloginfo(‘stylesheet_url’)
bloginfo(‘rss2_url’)
bloginfo(‘template_directory’)
bloginfo(‘home’)
Now multiply that by every page load per visitor. This in turn can save thousands of extra database queries depending on the number of traffic you get and improve your site’s performance a little. When you are on a shared hosting plan (which most of you will be when you first start out) it helps increase your page load time and also takes a little bit of pressure off the CPU.

How to go about doing this?

It’s just a matter of finding what each one of these queries retrieve from the database then replace that query string with the static info. I normally “echo” the output of all these queries and then take that output and replace the query with it. You can probably put the following in one of your theme’s file and find what the output of each one of these queries are then simply search and replace them:
echo bloginfo(‘html_type’);
echo bloginfo(‘charset’);
echo bloginfo(‘name’);
echo bloginfo(‘version’);
echo bloginfo(‘stylesheet_url’);
echo bloginfo(‘rss2_url’);
echo bloginfo(‘template_directory’);
echo bloginfo(‘home’);

Move Javascript at Bottom of the Page

Your site can run very slow or stall if another site that you call Javascript from is down. Moving the Javascript to the bottom of the page allows your visitor to still see your page content even though the javascript at the bottom is not loading for some unknown reason.
Moving the javascript to the bottom of the page is a common tip but I sometimes get asked the following question:
“How do I move Javascript to the bottom of the page exactly?”
So I am going to quickly cover this here. There may be exceptions where you specifically want to load the javascript in the header but generally it should be moved to the bottom of the page (footer of your theme). The HTML code to load a javascript looks similar to the following:
<script type=”text/javascript” src=”http://www.your-site.com/wp-content/themes/theme-name/scripts/some-script-file.js”></script>
So you can open your theme’s “header.php” and search for the word “javascript”. Once you find a line similar to the one above, cut that full statement (opening tag to end tag) and paste it in the “footer.php” file.

No comments:

Post a Comment