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’);

This link has a few examples and can be useful when doing this.

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.

Comments (23 responses)

  1. Towid Nategheian says:

    first trick is certainly useful for CPU optimizing. but second one is a common html page optimizing trick.
    of all ways of optimizing a WP page Caching was best. also using gzip greatly increases performance.

  2. Brilliant yet simple tips! I’m surprised there isn’t a plug in in WordPress to find and replace all these query items for you. I can imagine it would speed up a popular blog very much.

  3. Mark Sim says:

    You should try not to put the JavaScript code at the bottom of a page. It will take some time for the code to load and your website may not look completed when a visitor is looking at it. Thank for the WordPress blog optimization tips, though.

  4. This improved my blog layout very much, thanks for those 2 tips

  5. smhcarhire says:

    Nice tips and easy to implement..thanks for sharing

  6. sam says:

    these tips are really good and quick to implement, thanks for that!

  7. Matt says:

    This is great material and has helped me in several ways. I have tried to find this information elsewhere, but other sites just confuse things and make them harder than they actually are. Thanks for the help!

  8. John Gamings says:

    Nice tips. Unfortunately, this can make your theme nonportable, which can be a real hassle. Still interesting though

  9. tanhak says:

    i have so many problems in my website .. getting so many times in loading

    can any one friendly solve it .. i send it header.php and footer.php .. can he sets to me in a right way .. please ..

  10. admin says:

    If it’s a plugin then it’s best to request the plugin developer to do it otherwise the changes you make will get lost during an upgrade of that plugin.

    But if you want to change the code of the plugin then look for a line similar to this:

    add_action('wp_head', 'function_name....');

    You will see in the “function_name….” it has included those scripts. Simply move them to the footer using another action called “wp_footer”.

  11. Amber says:

    I was able to move the jscript to the bottom of my theme file, but Yslow tells me two e-commerce plugin files still have jscript at the top. Where should the script be moved when it’s in one little plugin file like that?

  12. Amber says:

    If yslow is telling me that jscript from a plugin is in the page header, (for example, “http://www.nsvintage.com/main/wp-content/plugins/wp-e-commerce/share-this.php”), to where should that jscript be moved?

  13. Jasmin says:

    Thanks for the grat work. After searching nearly 1 hour I found what I was looking for.)

  14. 2base tl says:

    I found your blog on google and read a few of your other posts. I just added you to my Google News Reader. Keep up the good work Look forward to reading more from you in the future.

  15. Charles says:

    I’ve always wondered if WP super cache interfered with Adsense on a page. If the whole page is made static then the ads won’t change??

  16. admin says:

    WP Super Cache is really good and has it’s usage but remember there is no one solution that fits everyone’s need. For example, I don’t use wp super cache myself because of other complications.

  17. Bob says:

    I wouldn’t recommend doing this! This makes your theme non-portable. Also, if you relocation your blog, e.g. directory change, change to domain (www.mydomain.com to blog.mydomain.com, etc.), you’ll be in trouble.

    If you’re worried about performance, just enable wp-cache or wp-super-cache.

    Don’t tweak your theme, stay generic

  18. Jane says:

    The tips are very helpful for me. Thankyou.

  19. Caroline says:

    Thanks for sharing. I really like this blog!

  20. John Michael says:

    Wonderful tips ..

    thank you
    regards
    john

  21. Kenny Meyer says:

    Thanks for sharing this… I like tweaking my wordpress-blog and this is very helpful in doing so.

Speak Your Mind

*