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:

{ 37 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!

MyAvatars 0.2 EverCleanse 01.12.09 at 1:01 am

I’m off to play Guitar Hero.

Seriously, thanks for the help with the .htaccess file. That shit screws me up six ways from Sunday. Cheers!

MyAvatars 0.2 TylerD 02.10.09 at 4:06 pm

Thank you MadHat! It’s amazing this problem still exists, but I’ve finally accepted the fact that I’ll have to hack the core. Oh well. Thanks again!

MyAvatars 0.2 Dimitris 02.13.09 at 8:39 pm

I tried your code but the 404 problem for /page/2/ is still there. I can’t find a global solution anywhere!

MyAvatars 0.2 Curt 02.21.09 at 7:35 am

good stuff man. this is very helpful for me.

MyAvatars 0.2 Shaw 02.21.09 at 7:49 am

Just thinking but, really, how bad would it be if Pages and Posts just couldn’t have the same names? Then, both could co-exist in peace and harmony.

MyAvatars 0.2 Sharon 02.22.09 at 5:35 am

The category base plug ins all works wonderfully, but for all of them you have to fix the ht access because these plug ins fixes your blog and not your blog’s other plug in.

MyAvatars 0.2 Lyndsey 02.24.09 at 1:38 am

Useful info, I guess, but this section is for proposing ideas for ways that Word press can be improved. Hacks are hard to maintain through maintenance upgrades and, if the problem really is so easily solved as your hack implies, then it should be fixed properly.

MyAvatars 0.2 Pete 03.02.09 at 12:01 pm

Hi,
am I correrct with the assumption that there is still no solution for the 404’s on subcategory archive pages when removing the category base in wordpress? Please correct me if I am wrong and someone finally figured it out!
Other than that Advanced Permalinks Plugin is a convenient way to set these things up…

Cheers
Pete

MyAvatars 0.2 Mykun 03.13.09 at 7:56 am

MyAvatars 0.2 Mark 05.18.09 at 8:41 am

This hack owrks a treat! Thanks for taking the time to share it with us.. ;)

MyAvatars 0.2 Funny SMS 05.29.09 at 7:20 pm

Hi,
Previously I was using a plugin called “Top Level Cats” for the same purpose. When I tried to upgrade WP to the latest version, that plugin started giving problem.

Thanks to your tip, I have now upgraded to WP 2.7.1 and everything is working perfect. Thank you so much.

{ 1 trackback }

MyAvatars 0.2 SEO ROI » Link Love Time! SEO Forecasting, SEM Tools, Free Ivy League Business Courses And More
05.24.09 at 8:17 pm


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>