How To Speed Up WordPress

As a side note, these are not ordered by importance or any criteria, I’ve just gathered everything I’ve learned about speeding up page loads on WordPress and compiled them here.

I guarantee that using even a few of these will drastically speed up your site.

1. Choose a good host
While starting out, a shared host might seem like a bargain (“Unlimited page views, wowie zowie!”), it comes at another price: incredibly slow site speed and frequent down time during high traffic periods.

If you plan on doing awesome stuff (aka the kind of stuff that creates high traffic periods), you’re killing yourself by running your WordPress site on shared hosting.

The stress of your site going down after getting a big feature is enough to create a few early gray hairs: don’t be a victim, invest in proper hosting.

2. Start with a solid framework/theme
You might be surprised to here this, but the Twenty Ten/Twenty Eleven “framework” (aka the default WP themes) are quite speedy frameworks to use.

That’s because they keep it the “guts” simple, and light frameworks are always the way to go to have a speedy site.

3. Use an effective caching plugin
WordPress plugins are obviously quite useful, but some of the best fall under the caching category, as they drastically improve page loads time, and best of all, all of them on WP.org are free and easy to use.

By far my favorite, bar none, is W3 Total Cache, I wouldn’t recommend or use any other caching plugin, it has all of the features you need and is extremely easy to install and use.

Simply install and activate, and what your page load faster as elements are cached.

4. Use a content delivery network (CDN)
All of your favorite big blogs are making use of this, and if you are into online marketing using WordPress (as I’m sure many of my readers are) you won’t be surprised to here that some of your favorite blogs like Copyblogger are making use of CDN’s.

Essentially, a CDN, or content delivery network, takes all your static files you’ve got on your site (CSS, Javascript and images etc) and lets visitors download them as fast as possible by serving the files on servers as close to them as possible.

5. Optimize images (automatically)
Yahoo! has an image optimizer called Smush.it that will drastically reduce the file size of an image, while not reducing quality.

However, if you are like me, doing this to every image would be beyond a pain, and incredibly time consuming.

Fortunately, there is an amazing, free plugin called WP-SmushIt which will do this process to all of your images automatically, as you are uploading them. No reason not to install this one.

6. Optimize your homepage to load quickly
This isn’t one thing but really a few easy things that you can do to ensure that your homepage loads quickly, which probably is the most important part of your site because people will be landing there the most often.

Things that you can do include:

  • Show excerpts instead of full posts
  • Reduce the number of posts on the page (I like showing between 5-7)
  • Remove unnecessary sharing widgets from the home page (include them only in posts)
  • Remove inactive plugins and widgets that you don’t need
  • Keep in minimal! Readers are here for content, not 8,000 widgets on the homepage

Overall, a clean and focused homepage design will help your page not only look good, but load quicker as well.

7. Optimize your WordPress database
I’m certainly getting a lot of use out of the word “optimize” in this post!

This can be done the very tedious, extremly boring manual fashion, or…

You can simply use the WP-Optimize plugin, which I prefer.

This plugin lets you do just one simple task: optimize the your database (spam, post revisions, drafts, tables, etc.) to reduce their overhead.

I would also recommend the WP-DB Manager plugin, which can schedule dates for database optimization.

8. Disable hotlinking and leeching of your content
Hotlinking is a form of bandwidth “theft.” It occurs when other sites direct link to the images on your site from their articles making your server load increasingly high.

This can add up as more and more people “scrape” your posts or your site (and especially images) become more popular, as must do if you create custom images for your site on a regular basis.

9. Add an expires header to static resources
An Expires header is a way to specify a time far enough in the future so that the clients (browsers) don’t have to re-fetch any static content (such as css file, javascript, images etc).

This way can cut your load time significantly for your regular users.

You need to copy and paste the following code in your root .htaccess file:

ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000

The above numbers are set for a month (in seconds), you can change them as you wish.

10. Add LazyLoad to your images
LazyLoad is the process of having only only the images above the fold load (i.e. only the images visible in the visitor’s browser window), then, when reader scrolls down, the other images begin to load, just before they come into view.

This will not only speed you page loads, it can also save bandwidth by loading less data for users who don’t scroll all the way down on your pages.

To do this automatically, install the jQuery Image Lazy Load plugin.

11. Control the amount of post revisions stored
WordPress, left to its own devices, would store every single one of these drafts, indefinitely.

Now, when this post is done and published, why would I need all of those drafts stored?

That’s why I use the Revision Control plugin to make sure I keep post revisions to a minimum, set it to 2 or 3 so you have something to fall back on in case you make a mistake, but not too high that you clutter your backend with unnecessary amounts of drafted posts.

12. Turn off pingbacks and trackbacks
By default, WordPress interacts with other blogs that are equipped with pingbacks and trackbacks.

Every time another blog mentions you, it notifies your site, which in turn updates data on the post. Turning this off will not destroy the backlinks to your site, just the setting that generates a lot of work for your site.

For more detail, read this explanation of WordPress Pingbacks, Trackbacks and Linkbacks.

13. Replace PHP with static HTML, when necessary
This one is a little bit advanced, but can drastically cut down your load time if you are desperate to include page load speeds.
There are three main things which will slow down your site for visitors:
1.Executing PHP commands
2.Pulling information from the database
3.Downloading graphics, javascript and other embedded elements

Advertisements

Wordpress – custom form submission redirect to 404 page.

I found many people having this problem in WP. The problem was caused by the “global” parameter. Obviously, WP has something important internal reserved for that parameter.

Global variable

Global variable is a variable that is accessible in every scope, in PHP it works ONLY for the same page and the file that are included after. However, some predefined variables, known as superglobals are always accessible in whole site. Both of global and superglobal variable can be redefined or overwrite it’s value.

When developing a WordPress Plugin and or Theme, sometimes you use the PHP global variable. That’s okay, but it’s HIGHLY RECOMMENDED that you not use variable name that are already defined by WordPress. Why?  Because it may break your other code that may intended to use WordPress variable.

Here is the lists of WordPress Reserved Variable, the left side is variable name, and the right side is the type

$_template_file = string
$require_once = boolean
$posts = array
$post = object
$wp_did_header = boolean
$wp_did_template_redirect = NULL
$wp_query = object
$wp_rewrite = object
$wpdb = object
$wp_version = string
$wp = object
$id = integer
$comment = NULL
$user_ID = integer
$cat = string
$paged = integer
$error = string
$m = integer
$p = integer
$post_parent = string
$subpost = string
$subpost_id = string
$attachment = string
$attachment_id = integer
$name = string
$static = string
$pagename = string
$page_id = integer
$second = string
$minute = string
$hour = string
$day = integer
$monthnum = integer
$year = integer
$w = integer
$category_name = string
$tag = string
$tag_id = string
$author_name = string
$feed = string
$tb = string
$comments_popup = string
$meta_key = string
$meta_value = string
$preview = string
$s = string
$sentence = string
$fields = string
$category__in = array
$category__not_in = array
$category__and = array
$post__in = array
$post__not_in = array
$tag__in = array
$tag__not_in = array
$tag__and = array
$tag_slug__in = array
$tag_slug__and = array
$ignore_sticky_posts = boolean
$suppress_filters = boolean
$cache_results = boolean
$update_post_term_cache = boolean
$update_post_meta_cache = boolean
$post_type = string
$posts_per_page = integer
$nopaging = boolean
$comments_per_page = string
$no_found_rows = boolean
$order = string

How do you get cruft free URIs for search results?

To return clean URIs for search results in WordPress from the search form (www.example.com/search/searchterms instead of http://www.example.com/?s=searchterms)

First create a file called search.php which contains:

header('Location: http://www.example.com/search/' . $_GET['s']);

Put that file in the root of your WordPress install and alter the action for the search form to this:

action="/search.php"

WP – I’ve been hacked. What do I do now?

The Exploit Scanner plugin can help detect damage so that it can be cleaned up. Other things you should do:

  • Change passwords for all users, especially Administrators and Editors.
  • If you upload files to your site via FTP, change your FTP password.
  • Clear FTP logs on your local machine, especially if you are using FileZilla on Windows
  • Re-install the latest version of WordPress.
  • Make sure all of your plugins and themes are up-to-date.
  • Check permissions of your files/folders in the install.
  • Check .htaccess file (Apache) for any additional rules added.
  • Update your security keys.
  • See FAQ My Site Was Hacked.

Moving WordPress Site

There are times when you need to move WordPress around within your server, and times when you need to move WordPress from one server to another. You don’t need to reinstall. WordPress is flexible enough to handle all of these situations.

When Your Domain Name and URLs Do Not Change

Moving your domain without changing the Home and Site URLs of your WordPress site is very simple, and in most cases can be done by moving the files.

  • If database and URL remains the same, you can move by just copying your files and database.
  • If database name or user changes, edit wp-config.php to have the correct values.
  • If you want to test before you switch, you must temporarily change “siteurl” and “home” in the database table “wp_options” (through phpMyAdmin or similar).

If you had any kind of rewrites (permalinks) setup you must disable .htaccess and reconfigure permalinks when it goes live.

When Your Domain Name or URLs Change

When your domain name or URLs change – i.e. from http://example.com to http://newexample.com – there are additional concerns. The files and database can be moved, however references to the old domain name or location will remain in the database, and that can cause issues with links or theme display.

If you do a search and replace on your entire database to change the URLs, you can cause issues with data serialization, due to the fact that some themes and widgets store values with the length of your URL marked. When this changes, things break. To avoid that serialization issue, you have two options:

  • Only perform a search and replace on the wp_posts table.
  • Use the Search and Replace for WordPress Databases Script to safely change all instances. ( If you are a developer, use this option. It is a one step process as opposed to the 15-step procedure below )

IE10 CSS Hacks

@media -ms-high-contrast Hack

This one comes from a gist by German developer Alex Kloss. It takes advantage of two things: 1) The fact that IE10 supports media queries; and 2) The fact that IE10 supports -ms-high-contrast as a media query option, which IE9 doesn’t support.

  1. @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
  2.    /* IE10-specific styles go here */
  3. }


According to Alex, this targets IE10 in both high contrast and default mode, which will evidently cover all stable versions of IE10.

This hack doesn’t work in older Platform Preview releases, because it seems that -ms-high-contrast wasn’t supported in those. I can confirm that it doesn’t work in IE10PP2.

Also, as Alex points out in the comments on the gist, this will also target IE11, so again this may not be a great choice.

@media Zero Hack

Finally, this one will target both IE9 and IE10, but nothing else. Not exactly ideal, but it’s an option if you need it.

  1. @media screen and (min-width:0) {
  2.     /* IE9 and IE10 rule sets go here */


This one might eventually be the best choice, but maybe not at the moment. Eventually, IE9 users are supposed to get notified via Windows Update to upgrade to IE10. This is supposed to include Windows 7 users. If this happens (and I’m not holding my breath), eventually IE9′s market share will be taken over by IE10, the same way this happens with other browsers that auto-update.

And in addition to that, if this parsing bug is fixed in IE11, then that means it will also be future proof. But we won’t know until IE11 is released.

Create Custom Post Type with category

A custom post type can be added to WordPress via the register_post_type() function. This function allows you to define a new post type by its labels, supported features, availability and other specifics.

add_action('init','postype');
function postype() {
$labels = array(
'name' => _x('Postype', 'post type general name'),
'singular_name' => _x('Postype', 'post type singular name'),
'add_new' => _x('Add New Postype', 'New Postype'),
'add_new_item' => __('Add New Postype'),
'edit_item' => __('Edit Postype'),
'new_item' => __('New Postype'),
'view_item' => __('View Postype'),
'search_items' => __('Search Postype'),
'not_found' =>  __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
'rewrite' => array('slug' => 'postype'),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','page-attributes','thumbnail','custom-fields','excerpt','comments'),
'taxonomies' => array('post_tag'),
'has_archive' => true
);
register_post_type( 'postype' , $args );
}

// Creating custom Taxanomy
add_action( 'init', 'create_postype_category' );

function create_postype_category() {
$labels = array(
'name' => _x( 'Location', 'taxonomy general name' ),
'singular_name' => _x( 'Location', 'taxonomy singular name' ),
'search_items' =>  __( 'Search Location' ),
'all_items' => __( 'All Location' ),
'parent_item' => __( 'Parent Location' ),
'parent_item_colon' => __( 'Parent Location:' ),
'edit_item' => __( 'Edit Location' ),
'update_item' => __( 'Update Location' ),
'add_new_item' => __( 'Add New Location' ),
'new_item_name' => __( 'New Location' ),
);

register_taxonomy('postype-category','postype',array(
'hierarchical'  => true,
'show_ui'         => true,
'labels'         => $labels
));
}