[wp-hackers] Cruft-free categories in 2.x?

Stewart Ugelow stewart at ugelow.com
Mon Mar 20 22:10:55 GMT 2006

Like many on this list, I decided to take the upgrade plunge to the
2.x branch after the latest security updates. In doing so, I've run
into some of the issues that Alex King describes at

I've found workarounds for most of the issues, but my white whale is
how to coax the new rewrite rules into not requiring /category before
all my %category% permalinks. Based on the documentation I've read,
the following plugin code should do the trick, but doesn't. I'm
clearly missing some crucial piece of the puzzle. Anyone out there who
can shed some insight?

function category_rewrite_cleanup() {
	global $wp_rewrite;
	global $wp_query;

	//load the rewrite rules
		$swap = $wp_rewrite->wp_rewrite_rules();
	//generate the "official" category rules
		$cat_perm = $wp_rewrite->get_category_permastruct();
		$cat_rules = $wp_rewrite->generate_rewrite_rules($cat_perm);
	//remove the official rules from the swap	
		foreach ($cat_rules as $key=>$value) {
	//define my rules
		$my_rule['(.+?)/?$'] = 'index.php?category_name=$matches[1]';
	//add them to the swap
		$swap = array_merge($swap,$my_rule);
	//reset the rules with the swap value
		$wp_rewrite->rules = $swap;

	//set the category structure
		$wp_rewrite->category_structure = '/%category%';

	//set verbose rules	
		$wp_rewrite->use_verbose_rules = true;

// Hook in.
     add_filter('init', 'category_rewrite_cleanup');

Many thanks,


Stewart Ugelow

More information about the wp-hackers mailing list