Removing the Dreaded Wordpress “Category” Forever!

Who thought it could be so difficult to remove 8 little letters?!?

I recently turned my back on Drupal and installed Wordpress 2.0.5. It isn’t perfect, but it’s much easier to skin (to me anyway).

One of Wordpress’s idiosyncrasies is it appends what it calls a “category base” to each of your posts. So when you want your URL to look like this:

http://www.myblog.com/category_name/post_title.htm

It makes it look like this:

http://www.myblog.com/category/category_name/post_title.htm

Annoying!

Yes, you can change what this word is under options > permalinks area of the admin. But what if you want to get rid of it all together?

After a little digging, I discovered the answer! A simple one line hack that solves my category base woes forever!

On line 36 of the wp-includes/template-functions-category.php file, you’ll see the code:

$catlink = str_replace(’%category%’, $category_nicename, $catlink);

To remove “category” from the URL, simply add this line under it:

$catlink = str_replace(’category/’, ‘’, $catlink);

That’s it! Try it out!

  • UPDATE

    After some testing, I found that you must have "%category%" in your permalink structure for this to work. Otherwise, your category pages will break.

  • Help Me Improve!

  • The only way for my posts to get better is to get your opinion! So help me out, and rate this one! Thanks!
    17 Votes | Average: 4.18 out of 517 Votes | Average: 4.18 out of 517 Votes | Average: 4.18 out of 517 Votes | Average: 4.18 out of 517 Votes | Average: 4.18 out of 5
    Avg. Rating: 4.18 - Total Votes: 17
    Loading ... Loading ...



16 Responses to “Removing the Dreaded Wordpress “Category” Forever!”

  1. Caio Proiete says:

    Hi Brian,
    Nice hack! Thanks for sharing.

    Although your approach works fine, I guess it would be better not to do a string replace on the category link string, but instead, modify the function that actually build permalink structure. A few lines above your modification, you can see a call to get_category_permastruct() that is the function who brings us the annoying “category/” on the link.

    Here is my version of the same hack:

    On line 371 of the wp-includes/rewrite.php file, you’ll see the code:

    $this->category_structure = $this->front . ‘category/’;

    Now just remove the “category” from the string, and the code should look like:

    $this->category_structure = $this->front . ‘/’;

    Done… The same result, but a bit more elegant.

    Caio Proiete
    www.pdaexpert.net

  2. irawan says:

    Hi Brian,
    Thank You very much for the shared tricks, I had used Caio Proiete’s ‘hack’ before, it worked ! But after I restart my laptop (I tested first in localhost), it said 404 for all pages .. so sad.

    Miraculously .. I found this tips..

    again thank you, I’ll touch you after my blog is up

  3. Brian says:

    UPDATE

    I used the method described by Caio Proiete and was having some issues with links inside of the various categories returning a 404 error. I then went back to the original method and it worked.

    Strange. I don’t know why it worked and the other way didn’t, but I thought it worth mentioning.

  4. Mike says:

    My God in heaven! I’m practically bald from pulling my hair out on this one. Thank you for the great solution. Like others, Caio’s approach didn’t work for me, but this works like a charm.

  5. Brian says:

    Hey Mike. Glad it helped. I too lost a few years of my life on this one.

  6. farida says:

    Hi
    see this 5 step tutorial that tells you how to remove category base form permalinks. This is only for wordpress 2.0+ the only difference is that it uses category-template.php.
    http://bloggerholic.com/wordpress/how-to-remove-category-base-from-permalinks-45.htm

  7. Блог unsimilar’а » Blog Archive » О геноциде префикса “category” says:

    […] Кому интересно посушить мозги, вот ссылочки: Change or remove the “Category” from your permalinks on a wordpress blog How to remove category base from permalinks Removing the Dreaded Wordpress “Category” Forever! WordPress Ideas: Remove ‘Category Base’ From Permalinks WordPress Support: how could i remove word “category” from urls? […]

  8. Category Base in Wordpress deaktivieren - WhiteSide SEO says:

    […] Brian Shoff schlägt zur Entfernung der Category Base in einem anderen Artikel noch einen Eingriff in den Code vor und überall sind Hacks im Einsatz. […]

  9. Remove The Word Category From Wordpress URLS | Social CMS Buzz says:

    […] Massive thanks to brianshoff.com for the original code snippet. […]

  10. sunilkumar says:

    Hi,
    It worked for me when i did as you said
    but i am getting the links as http://www.localsadda.com/softwares
    The last slash is not coming.
    How can i get the ‘/’ at the end of the category i.e., after word softwares in the above link.

    Regards,
    Sunil.

  11. Brian says:

    It’s questions like these that make me wish I was a *real* programmer. ;-)

    To take a stab at it, as I don’t have time at the moment to test this, but wanted to get back to you quickly… buuuuut… next to the catlink in the str_replace function, add:

    . ‘/’

    This *might* append the trailing slash you’re looking for.

    Or, if you are slick with .htaccess, you could do a 301 redirects of sorts to accomplish the same thing (I’m about 50/50 on that suggestion)

    Hope this helps a little. :-/

  12. jrgd says:

    i think you could also use the permalink settings in your wp-admin to change the way the url is generated for your categories and tags; i changed it the tag permalink into “discover_more”:

    http://r-echos.net/discover_more/code/

    and so far it works fine - i guess changing it for the category work - the same. this solution, like your plugin, has also the advantage of keeping your install updatable without too much hassle but it

  13. Kevin says:

    Any ideas on how to make this work if we are just using %postname% for our permalink structure (no category in permalink)?

    I am getting he following error because I am not including categories in my permalink:
    Warning: Division by zero in wp-includes/category-template.php on line 43

    Oh, and can you add a “subscribe to comments” plugin feature so I will be updated when you comment back please? Thanks.

  14. Aaron says:

    I’m getting 404’s on any /category/page/2/ url’s…any idea how to work around that?

  15. Aaron says:

    Nevermind….figured it out!

  16. Aaron says:

    I’ve got the pagination problem fixed if anyone is having that issue.

    http://www.themadhat.com/blogging/remove-category-base-wordpress/

Leave a Comment

Creative Commons License

This work is licensed under a
Creative Commons Attribution 2.5 License.
34 queries. 1.312 seconds.