NELSON PIRES

rants, ranks and (r)evolutions

Remove word “category” from WordPress URLs

This question seems to pop up all the time around WordPress related forums and blogs, and it’s one of those that I have found a fix for and use all the time. 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.

The problem

After a fresh WordPress installation, you 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 because this file gets overwritten every time you update WordPress. 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 to do it:

For WordPress up to v3.1, change this:

/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 this:

/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 sometime, 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 can be found here (http://wordpresssupplies.com/wordpress-plugins/no-category-base/) and it’s also listed at the official WordPress site (http://wordpress.org/extend/plugins/wp-no-category-base/). The change log reveals a healthy update frequency but above all it’s great because it just works.

Leave a comment, let me know what you think...
comments powered by Disqus