Remove The Category Base From WordPress

by TheMadHat on October 31, 2008

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 }

Marty Martin October 31, 2008 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::

streko October 31, 2008 at 3:19 pm

makes me wanna play guitar hero.

Andy November 4, 2008 at 1:50 pm

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

Vahid November 4, 2008 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 :)

TheMadHat November 4, 2008 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.

Edgar November 4, 2008 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

TheMadHat November 4, 2008 at 4:37 pm

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

purposeinc November 5, 2008 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

TheMadHat November 5, 2008 at 12:53 am

That’s what I was getting at… :)

muzammal November 6, 2008 at 12:32 am

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

craig.c November 6, 2008 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 ..?

TheMadHat November 6, 2008 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?

SEO Binh Nguyen November 6, 2008 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.

TheMadHat November 7, 2008 at 12:09 pm

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

Sankar Datti November 13, 2008 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

Shaneen Clarke November 18, 2008 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.

Prue November 22, 2008 at 5:15 am

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

WebDuDe November 27, 2008 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

TheMadHat November 27, 2008 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.

daidamia November 28, 2008 at 2:58 am

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

Prashant December 10, 2008 at 12:17 am

Why not just use the custom structure in wordpress admin?

TheMadHat December 10, 2008 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.

ED December 17, 2008 at 8:37 pm

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

JD December 25, 2008 at 1:54 am

Hey Dude !

This is really very useful information. Keep it sharing.

Kalyan Online January 2, 2009 at 7:07 pm

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

Calvinj January 5, 2009 at 11:36 pm

Haha good stuff thanks!

EverCleanse January 12, 2009 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!

TylerD February 10, 2009 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!

Dimitris February 13, 2009 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!

Curt February 21, 2009 at 7:35 am

good stuff man. this is very helpful for me.

Shaw February 21, 2009 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.

Sharon February 22, 2009 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.

Lyndsey February 24, 2009 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.

Pete March 2, 2009 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

Mykun March 13, 2009 at 7:56 am
Mark May 18, 2009 at 8:41 am

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

Funny SMS May 29, 2009 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.

Comments on this entry are closed.

{ 1 trackback }

Previous post:

Next post: