NELSON PIRES

rants, ranks and (r)evolutions

Remove word “category” from WordPress URLs

I don't use WordPress anymore for my development but this question seems to pop up all the time around WordPress related forums and blogs, and it’s one of those that I have developed a fix for and use all the time on legacy sites still running on WordPress. I’m very surprised the WordPress team does not come up with a simple solution for this, there’s clearly an audience eager to have it. In the mean time here's my fix and it works just fine.

The problem

After a fresh WordPress installation, one of the first things you should do is to head up for the Permalink Settings to change the url structure and usually I use this as custom: /%category%/%postname%/

This allows to show the category name of the post as a directory and this structure is most sensible and preferred as it provides some sort of hierarchy, the problem really becomes apparent when you view the category page, it has the word “category” prefixed to the category name. So if we have a category named “web” the url will become .../category/web/ when you want just .../web/.

Leaving the word “category” there is a waste of url real estate, and conveys no meaning what so ever.

The solution

The fix I developed requires changing a WordPress system file and I must admit this is not the ideal type of fix because this file gets updated and overwritten every time you update WordPress it-self. Strangely it seems to be the only way to achieve this while using the custom permalink structure described above.

(see update below)

Here’s how:

For WordPress up to v3.1, change:

/wp-includes/category-template.php on function get_category_link() line 18

Change the return of the function:

From:
return apply_filters('category_link', $catlink, $category_id);
To:
return str_replace("category/", "", apply_filters('category_link', $catlink, $category_id));

For WordPress v3.1 (and possibly above), change:

/wp-includes/taxonomy.php on function get_term_link() line 2820

Change the return of the function:

From:
return apply_filters('term_link', $termlink, $term, $taxonomy);
To:
return str_replace("category/", "", apply_filters('term_link', $termlink, $term, $taxonomy));

If you are going to do this after your blog has been around for some time, don’t forget to do a 301 redirect from all /category/ urls to the new cleaner equivalents, use this in your access rules:

RewriteRule   ^category/(.+)$   http://www.yourdomain.com/$1   [L,R=301]

You should also be aware that now there’s no difference between what is a page and what is a category, so make sure your pages are not named the exact same as your categories. This is the reason the word /category/ was there in the first place, to provide some sort of separation. However I would’ve preferred the pages to be prefixed instead with /page/ or something like that as opposed to having the categories prefixed.

So there you go. Let me know if you found a better way to achieve this specially at theme or plugin level so we don’t have to change the function every time we update WordPress.

UPDATE

It seems a plugin was found (thanks iDope). The plugin is called “WP No Category Base” and it’s available at the official WordPress site. The change log reveals a healthy update frequency but above all it’s great because it just works.

Leave a comment below and let me know what you think...

NOTE: For complex or long technical questions email me directly.

comments powered by Disqus