Essential WordPress Performance Optimization 101

7

Essential WordPress Performance Optimization - 101 - Geekswipe

It’s been great reading all your feedbacks this week. But most of you rather had different questions regarding Geekswipe’s WordPress configuration. Though I never had written any WordPress tweaks or hacks, I thought I could sum up all that in this edition.

Although this article is intended for the WordPress installations on shared servers, this could benefit anyone who is into optimizing WordPress or just love to help out others.

The basic stuffs

As most of you geeks are already familiar with WordPress, I’m going to tread over the basic stuffs assuming that you might’ve done it already. This includes your image optimization techniques, caching settings and using a good CDN.

Image Optimization

Nothing much here. All images are optimized with the ‘save as web’ feature on PS before uploading to WordPress. For consistency, I use a standard size for all the featured images. The thumbnails are altered to match the aspect size too. And once the image is uploaded, it’s further optimized using WP-Smush – one of the good image optimizing plugins. So that’s all about your images.

Caching

I’m heavily biased with W3 Total Cache here. It’s so awesome, providing a plethora of options to optimize better. There are of course a few options that could be a double-edged sword on shared environments, but you would easily understand it if you are here reading this article. Not a big thing! Most of the times, minification helps improve the performance but breaks several front-end stuffs. So it depends on your theme, and it is solely your decision. Being said that, if you are caching your static resources, most likely you will end up with versions of CSS and JS files. It is always a good practice to remove those query strings, as with such versions echoed, proxies and CDNs will hardly cache them. The optimal way to remove this is using the following code in your functions.php.

// Remove CSS and JS Versions
function remove_cssjs_ver( $src ) {
if( strpos( $src, '?ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );

CDN

Though you are expecting a moderate traffic at the beginning, it is always good to have an overkill. Stating CDN as an overkill is quite an exaggeration. It’s rather a basic optimization necessity these days. At least twice every week, I get hit by a paramount amount of traffic when any of my content gets on the hug of death wagon from StumbleUpon. If it weren’t for the CloudFlare CDNs, I’d sure be facing significant performance red marks. Despite this, CDN has more advantages like DDoS protection, spam control, and even minification. So find a good CDN. For most of the cases, CloudFlare fits the best. Hey! It comes with a Flexible SSL support too.

With all these in mind, you should also vet the plugins you download. Not all plugins are developed with proper standards. Choose wisely.

In-depth optimization

Sometimes, even the regular techniques are not enough to keep your WordPress stable. I experience quite a lot of hiccups every day and only by digging up the problems, I have understood that the best way to optimize WordPress is from the inside. No! Not the core! Never touch the core! This section includes optimization suggestions and tweaks that are targeted for the WordPress backend.

Heartbeat

Always assume that your software and hardware environments are not perfect. Especially when it comes to a WordPress installation with whatnot plugins installed, running on a shared environment, most likely it will occasionally hiccup. Your server logs are the only best assistance in such hiccup cases. In my case, my servers go down mostly because of exceeding my process limit with too many overlapping processes (and that’s why you’d need a VPS or a dedicated machine). This mostly happens when you leave your administrator dashboard open for a long time, or say, editing your posts simultaneously with fellow authors and editors. WordPress keeps your connection alive so that the backend is in sync with your activities using the Heartbeat API. This obviously could create too many concurrent processes and cause chaos. To avoid this, you could use a simple heartbeat control plugin like this, or better turn it off if you are okay with that.

Dequeuing plugin stylesheets and scripts

This is the most frustrating thing about plugins. Even some popular plugins don’t use the proper ways to load the plugin specific stylesheets and scripts. For example, the popular contact form plugin, Contact Form 7 loads its stylesheets and scripts in all pages. This is an extra resource that you have to serve for all your visitors and every single non-cached requests from them. Now just imagine all your plugins doing the same. Unfortunately, not all developers are as cool as Contact Form 7 to give you a basic guide like this to remove the resources from unnecessary pages.

This is obviously a little painstaking process, as you have to go through all the plugin files to know the handles the author used to enqueue the scripts and styles. Once you get to know that handler, you can deregister or dequeue those styles and scripts similar to the following (for Contact Form 7) in your functions.php.

// Remove Contact Form 7 Stylesheet on all pages except contact
function cf_deregister_styles() {
if ( !is_page('contact') ) {
wp_deregister_style( 'contact-form-7' );
}
}
add_action( 'wp_print_styles', 'cf_deregister_styles', 100 );
// Remove Contact Form 7 JavaScript on all pages except contact
function cf_deregister_js() {
if ( !is_page('contact') ) {
wp_deregister_script( 'contact-form-7' );
}
}
add_action( 'wp_print_scripts', 'cf_deregister_js', 100 );

Hack your theme

As an extreme measure, you can hack your theme further to suit your needs. Most of the themes you download and use have some overkill functionalities that you won’t be using at all. Find it and purge it. Make sure you the theme license allows you to do that.

This includes modifying your thumbnail sizes and assigning crop methods to suit your featured image, cleaning up the newlines from the resource files, refactoring a few templates and functions and stuffs like that.

Plugin count

Theoretically, WordPress can run any number of plugins. But it is always advised for a shared server installation of WordPress to run on a minimum number of plugins as possible. Each plugin makes certain calls to the database, and this could cause a significant load on your server if it is not a good one.

Optimize database tables

This is a must thing to do on all WordPress installations, probably every month. Most of you would constantly install and test several plugins. When you delete them, they leave a whopping lot of tables behind, and it will stay there forever unless you delete them. WP-Sweep is the only best plugin I know that does the job the right way. Use it wisely too.

As far as I know, these major tweaks and tuning will be far enough to optimize your WordPress environment for better performance on a shared hosting or any hosting. I hope I answered most of your questions that you sent to me. Let me know if you have any other questions in the comments. If you need immediate answers check out the #wordpress channel on Freenode or Slack.

This post was first published on February 14, 2014.

Avatar

Karthikeyan KC

Aeronautical engineer, dev, science fiction author, gamer, and an explorer. I am the creator of Geekswipe. I love writing about physics, aerospace, astronomy, and python. I created Swyde. Currently working on Arclind Mindspace.

Leave a Reply

Your email address will not be published. Required fields are marked *

7 Responses

  1. Avatar Andre Hibbler

    Hi, your website looks great! Thank you for the tips. Is there any specific reason why you changed from W3TC to WP Super Cache now?

    • Thank you! I switched to Super Cache a few months ago over some performance issues with Total Cache. W3 Total Cache wasn’t updated for a while at that time. But both are great plugins IMO. :)

  2. TIL there is something called Heartbeat.

  3. Good info. Is performance a reason that you are using Disqus? I personally hate Disqus and I don’t feel very good about having my comments stored on somebody’s server! How does it affect your SEO?

    • Thanks :)

      1. I prefer Disqus for so many reasons. It’s robust and more or less found in almost 80% WordPress sites. Moreover, no serious conversations take place in the comment sections, and it just doesn’t work. This is one of the main reasons I installed Curiosity.

      2. Disqus uses JS to load up their widget, so I don’t think SEO wise this is good for any blogs.

      If not owning your comments is the only concern, try the Disqus WordPress plugin. You can sync your WordPress comments!

  4. Avatar Luke Melcash

    Thanks for this! What is the difference between `wp_dequeue_script` and `wp_deregister_script`? Both seems to work!

    • :) Welcome… ‘dequeue’ is just removing the script/style from the pages. But, ‘deregister’ is de-registering the entire script/style from the queue with the associated handle.

Related