Remove The Category Base From Wordpress

by TheMadHat on October 31, 2008

If you're new here, you must subscribe to my RSS feed, or I will hunt you down. Thanks for visiting!

Going CrazyThis might have been the most annoying thing I’ve had to do in a while. As you’re probably well aware, the pesky “category” level that shows up in your category page URL’s was practically impossible to get the hell off. There were several plugins sitting out there and none of them worked quite right. All the ones I found worked to some degree, but there was always some problem that I didn’t like.

You’ve got the Category Base Killer plugin which seemed to work fine at first, but pagination wouldn’t work and apparently there is a problem with having a subcategory or a category with a parent.

There is the Top Level Category plugin, which worked only if you added an extension to your post names. You’d end up with funky looking URLs like /category/post.html/ and that would require redirecting all of your posts. And again pagination didn’t work.

There were a few more custom coding solutions I came across that blew up my blog.

Here is what I did and I’ve fooled around with mine enough to where I think it works right. First some caveats:

1) I’m using the Thesis Wordpress Theme (great theme by the way). I didn’t change any code inside the theme so this shouldn’t matter.

2) I don’t use subcategories or parent categories so I don’t know if that works or not. I also don’t use multiple categories, but I don’t think that matters. I believe it just uses the first one anyway.

3) My permalink structure is set at /%category%/%postname%/ - if yours is different, you’ll probably need to tweak it.

4) This isn’t a plugin (yet, I might get around to doing that but I have never written a plugin before and I wanted to get this out now). This means when you upgrade Wordpress, you have to insert the code again. (Tip: I comment in // $$$ Custom code // where I put in my own stuff so it’s easy to find and fix when Wordpress updates)

So, here is the solution:

1) In wp-includes/category-template.php search for the following line of code:

$catlink = get_option('home') . user_trailingslashit($catlink, 'category');

Just below that, insert the following:

$catlink = str_replace('/category', "", $catlink);

2) In wp-includes/classes.php search for the following block of code:

if ( isset($error) )
$this->query_vars['error'] = $error;

Just below that, insert the following:

if (@$this->query_vars["name"] == "page")
{
$tmp_ar = $this->query_vars;
$this->query_vars = array();
$this->query_vars["paged"] = str_replace("/", "", $tmp_ar["page"]);
$this->query_vars["category_name"] = $tmp_ar["category_name"];
}

3) Finally, in your .htacces file, add the following to redirect your old /category/ URLs to your new ones (if your blog doesn’t reside in the root, you’ll have to tweak it):

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

Guitar Hero Is My FriendThere you have it. I don’t guarantee this will work for you, and if it doesn’t, well bite me because I tried. And just to make this post more interesting, here is a random hot chick with Guitar Hero.

 

 

 

 

Similar Posts:

{ 26 comments… read them below or add one }

MyAvatars 0.2 Marty Martin 10.31.08 at 3:12 pm

This is going to be really helpful for me, thanks for the extra effort. It would be great if someone could work it into a plug-in. Anyone? ::crickets::

MyAvatars 0.2 streko 10.31.08 at 3:19 pm

makes me wanna play guitar hero.

MyAvatars 0.2 Andy 11.04.08 at 1:50 pm

Thx Themadhat this creepy category base is much pain for me now, i’ll try your method.

MyAvatars 0.2 Vahid 11.04.08 at 4:22 pm

I tried it, and it did remove the category from the url, but I still got a 404 error when i went to the category, so no go for me. Thanks though :)

MyAvatars 0.2 TheMadHat 11.04.08 at 4:25 pm

@Vahid - yea, it could be conflicting with another plugin or something already in your .htaccess file. Either could easily be the case.

That’s why this is such a pain in the ass, too many variables to work with inside Wordpress.

MyAvatars 0.2 Edgar 11.04.08 at 4:31 pm

Jajaja thanks for this post I tried this on my blog and what would you know i got the categories removed. Nice ending for a post

MyAvatars 0.2 TheMadHat 11.04.08 at 4:37 pm

@Edgar. Good deal, glad it’s working for some.

MyAvatars 0.2 purposeinc 11.05.08 at 12:11 am

After reading the above post, being a heavy duty user of wordpress myself, and even knowing Matt Mulenweg and Doncha the original developers, it is my carefully stated opinion that putting the picture of the hot chick playing whatever she was playing was a good idea.

dk

MyAvatars 0.2 TheMadHat 11.05.08 at 12:53 am

That’s what I was getting at… :)

MyAvatars 0.2 muzammal 11.06.08 at 12:32 am

Thanks for sharing your effort. I’m learning wordress and would like to read more posts in future.

MyAvatars 0.2 craig.c 11.06.08 at 4:04 am

the WP theme i’m using doesn’t have theif ( isset($error) )
$this->query_vars['error'] = $error;
code in wp-includes/classes.php.

any suggestions in that instance ..?

MyAvatars 0.2 TheMadHat 11.06.08 at 10:29 am

@craig - you’re theme shouldn’t make any difference. Themes don’t modify files inside wp-includes/

What version of WP are you running?

MyAvatars 0.2 SEO Binh Nguyen 11.06.08 at 11:16 pm

I tried many solutions before. Now I try yours, and still… my sub category will be 404 when I use this post permalink: /%category%/%postname%/

I guess you don’t have time for this anymore. So just take it as a sigh. If you do have a solution, please email me. I will really appreciate. Thanks.

MyAvatars 0.2 TheMadHat 11.07.08 at 12:09 pm

I didn’t test it on sub categories. Stay tuned, the plugin might address that issue.

MyAvatars 0.2 Sankar Datti 11.13.08 at 7:12 am

Hello

At last I got it. Thank you so much :) This is what exactly I am suffering with. Good Post.

Regards
Sankar Datti

MyAvatars 0.2 Shaneen Clarke 11.18.08 at 9:13 pm

Thanks for this tweak. I’m thinking on switching to wordpress for my other sites but I’m still taking notes on some things I don’t like.

MyAvatars 0.2 Prue 11.22.08 at 5:15 am

Thanks for the info. i’ll try your method out…

MyAvatars 0.2 WebDuDe 11.27.08 at 11:01 am

im really interested in sorting the subcategories out
the “fix” works like a charm for main categories
but all subcats bring a 404 error

madhat, pls enlighten us with another fix for subcats

ahoy ye m8ies

MyAvatars 0.2 TheMadHat 11.27.08 at 11:56 am

@WebDuDe - A plugin for this is coming soon, I’ll see if we can get this addressed. I don’t use subcats so I didn’t put it in, but the plugin hopefully can do this. It’s a bit more complicated than it looks.

MyAvatars 0.2 daidamia 11.28.08 at 2:58 am

Post was interesting to read! I just want to try it out once.

MyAvatars 0.2 Prashant 12.10.08 at 12:17 am

Why not just use the custom structure in wordpress admin?

MyAvatars 0.2 TheMadHat 12.10.08 at 12:26 am

Because that doesn’t work. The way the hooks are written forces “category” on any category pages before the actual category. This gives you the proper site architecture.

MyAvatars 0.2 ED 12.17.08 at 8:37 pm

Thx Themadhat this creepy category base is much pain for me now, i’ll try your method.

MyAvatars 0.2 JD 12.25.08 at 1:54 am

Hey Dude !

This is really very useful information. Keep it sharing.

MyAvatars 0.2 Kalyan Online 01.02.09 at 7:07 pm

I followed the instructions and it worked for my website, thank you :)

MyAvatars 0.2 Calvinj 01.05.09 at 11:36 pm

Haha good stuff thanks!

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>