[wp-hackers] Some Thoughts/Enhancement Ideas In And AroundTheCategory Side Of Things

Michael Pretty mpretty at voceconnect.com
Thu Feb 11 03:26:52 UTC 2010


> I'll rephrase that then, WHAT EXACTLY does /taxonomy-name-here/ do? I 
> mean, at the end of the day you could have 100's of these setup, so 
> assisting with internal rewrite rules is still going to create a huge 
> long list am I not right? - Which defeats the purpose?
>
> So moving away from what it does, why can't we take advantage of it, 
> and make it useful? I had an interesting discussion with Michael 
> Fields earlier on about it, and he completely agrees! You can take a 
> look at what was said from @wpkid which is me, and @_mfields which is 
> Michael. If it's got to be included, then something can be done to 
> enhance it, but still keep it to greatly assist in some internal 
> rewrite rules that nobody seems to be explaining, but I'm sidetracking 
> now!

The rewrite rules for WordPress consist of an associative array of 
regular expressions mapped to query strings.  When a request is sent to 
WordPress, the WP class loops through these regular expressions to see 
which one matches the current URL structure.  When a match is found, the 
regular expression match results are used to fill in the query_vars for 
the main query based off of the query string of the matching regular 
expression.

So if the URL is /category/foo/, the matching regular expression of 
"category/(.+?)/?$" gets mapped to 
"index.php?category_name=$matches[1]"which then renders the query_vars 
of category_name=foo.  If the 'category/' prefix wasn't there, the 
corresponding regular expression for categories would just be 
'(.+?)/?'.  The problem with this is, that pages use the same regular 
expression to map to 'index.php?page_name=$matches[1].  So, without this 
'category/' identifier to make the regular expression different, 
WordPress would have no way to know whether the query_vars should be 
'page_name=foo' or 'category_name=foo'.

There was a suggestion to add rewrite rules for each specific category, 
in other words the regular expression for the 'foo' category would be 
'foo/?' which maps to 'category_name=foo'.   This would keep the 
category rules without the 'category/' prefix from colliding with the 
page_name rules.  Its a solution that will work for the most part, but 
it will have a distinct disadvantage of severely slowing down, if not 
breaking sites with a large number of categories.  This is why it can't 
be a part of core.  If it becomes a part of core to add a rewrite rule 
for every category, then sites with 100's of categories will also have 
100's of rewrite rules that will have to be looped through upon each 
request.  It just isn't an efficient solution, which is why this should 
stay in the plugin domain.

That said, I do think something could be thought of to improve the issue 
with site.com/category/ throwing a 404 by default.  Personally, I've 
always setup a page a /category/ that used a custom template that listed 
the categories to fill this spot.  No reason something can't be added to 
core to do the same.  Themes would have to support it though.

Michael
@prettyboymp


More information about the wp-hackers mailing list