New Clean Category Links

I just finished a new hack to b2evolution to have super-clean category links like
/category/tips_and_tricks instead of /blog?cat=103.

If you use b2evolution, and you want to know how to do this, read on.

If you don’t, then this post has nothing for you. Look and admire the awesome might of my clean category links and the geekery that spawned them!

Ok, to start off, this is a big deep ugly hack that involves changing a lot of stuff, creating a file or two, and modifying the db.

Not for the faint of heart, but worth it, for the cleanness of category links.

How To

  1. Use super clean .php-less links. Add “category” to the list of blog stubs in your .htaccess file.

    Important: DONT HAVE ANY BLOGS WITH A STUB OF “category” OR THIS WILL BREAK!

  2. Make sure that your categories all have unique names. It won’t work if you have two blogs with categories of the same name.

  3. Create a new column in your evo_categories table. Call it “cat_url_title”, and make it a Varchar(255) or so. (TinyText would work, too.)

  4. Create a file called “category.php”, and put this in it:

    [The requested file /home/yaohytmw/isaacschlueter.com/download/b2evo/category.php could not be found]

    Save it, and upload to your website.

    Now, urls like http://yoursite.com/category/my_category should get you the category listing. If it doesn’t work, then stop here, and start debugging. The category name in the url should be lower-case, with _ instead of space, -, \, or /. (Other funky chars could probably mess it up…)

  5. If you already have a /conf/hacks.php, add this too it. If not, create a file called “hacks.php” and put it in your conf folder. Put this in it:

    [The requested file /home/yaohytmw/isaacschlueter.com/download/b2evo/category_stub_link.php could not be found]

    Make sure that your blog still works. Replace the part that it says to replace with http://yoursite.com/category/.

  6. Open up b2evocore/_class_item.php, and find this line:

    $cat_name = '<a href="'.url_add_param( get_bloginfo('blogurl', $curr_blogparams), 'cat='.$cat_ID ).'" title="'.$link_title.'">'.$cat_name.'</a>';

    Comment it out, and replace it with this:

    $cat_name = '<a href="'.category_stub_link($cat_name).'" title="'.$link_title.'">'.$cat_name.'</a>';

    Now, you should have clean category links on each blog post. If you don’t, stop and debug.

  7. Open up skins/_categories.php, and find this line:

    echo '<a href="'.url_add_param( get_bloginfo('blogurl'), 'cat='.$cat_ID ).'">'.format_to_output($cat['cat_name'], ‘htmlbody’).’</a> <span class=”dimmed”>(’.$cat['cat_postcount'].’)</span>’;

    Comment it out, and replace it with this:

    echo '<a href="'.category_stub_link($cat['cat_name']).’”>’.format_to_output($cat['cat_name'], ‘htmlbody’).’</a> <span class=”dimmed”>(’.$cat['cat_postcount'].’)</span>’;

    Now, the category links in the sidebar of each skin should be cleaned, too. You may need to perform this step again in any skins that have highly customized category lists, but that’s not very common.

That’s all there is to it! :)

Further Steps

I’m not particularly happy with the lack of integration for this. It’s very much a “hack”, and here’s what I’d have to do with it before checking it into CVS to be a part of the b2evolution codebase:

  1. The cat_url_name should be created whenever the category is created, and should be configurable in the backoffice.

  2. The cat_url_title should be a key to prevent duplicates, and if a duplicate exists, then b2evo should save it with a number at the end (like with post_url_title.)

  3. It should sanitize the same way that post_url_title is sanitized, rather than my somewhat arbitrary selection of a few characters that aren’t allowed.

  4. There should be a place in the backoffice to specify the category stub name. Actually, http://example.com/blog_stub/category/cat_url_name would be a better (more semantic) implementation, imo. That way, there’s no extra cat stub, and the code that grabs the category would go in _vars.php where it slices up the URL. (The same place where I hacked it to take the date out of the permalinks.)

  5. I’d have to actually get PGP working properly.

All in all, more work than I have time for at the moment.

Leave a Reply

Comments are moderated like crazy using a variety of plugins. There is a very high likelihood that your comment won't show up right away, especially if you have never commented here before, but it was not deleted.

Please be patient, and do not post your comment more than once. It will show up once it is approved.

You must be logged in to post a comment.