WordPress: Automatically Remove Short Words In URL For Better SEO

WordPress: Automatically Remove Short Words In URL For Better SEO

According to Matt Cutts’s answer in his interview at Pubcon, for good SEO, the URL should contains 3-5 words. If there are more than 5 words, Google’s algorithms typically will just weight those words less and just not give you as much credit. So, we should keep our URL as short as possible, and remove all unwanted words.

Usually the short words in URL (with number of character not greater than 3) are not very good for SEO, and they often contain stop words like ‘a’, ‘an’, ‘the’, etc. They should be removed from the URL for better SEO. In WordPress, we can do it automatically without any plugin.

Just paste the following code into your functions.php file of your theme:

add_filter('sanitize_title', 'remove_short_words');

function remove_short_words($slug) {
    if (!is_admin()) return $slug;
    $slug = explode('-', $slug);
    foreach ($slug as $k => $word) {
        if (strlen($word) < 3) {
            unset($slug[$k]);
        }
    }
    return implode('-', $slug);
}

The function remove_short_words will hook into sanitize_title action. It takes the post slug as an argument, explode its words into an array. Then it check length of every element, if length < 3 then remove it. You can change 3 to any number of characters, but I think 3 is a good choice.

Now try to create a new post. Type the title and see it in action. For example, if I type in the title field: ‘This is a sample post’, then I’ll get ‘this-sample-post’ as post slug (URL). This is quite good, isn’t it?

Hope this small tip can help you improve your SEO score of your blog and get better position in SERPs.

7 thoughts on “WordPress: Automatically Remove Short Words In URL For Better SEO

  1. the latest version of wordpress, if you use this method, information is lost in the sidebar (in admin).

  2. I think there is a logical problem in code.
    When we write two posts like ‘WordPress 5 tips’ and ‘WordPress 10 tips’, the code will generate same url lik ‘wordpress-tips’ so one of posts will not be accessable.
    I am right, aren’t right?

  3. This code can solve a minor problem im having for a custom post. Can we alter this code for spesific custom post type to remove only ” – ” between words?

Comments are closed.

Newsletter

Subscribe to our newsletter to receive news and tutorials about WordPress and web development.

Meta Box - Creating expert WordPress custom fields and custom meta boxes in minutes! Check it out →