[wp-hackers] Improving rewrite flexibility

Jamie Talbot wphackers at jamietalbot.com
Thu Jan 12 20:59:09 GMT 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi All,

I've nearly finished work on a multiple languages plugin (phps attached
[still alpha - not for production sites!]) and have found a couple of
places in the code where I could do with a bit more flexibility, hook-wise.

The main area is rewrite_rules.  As it stands, I'm having to redefine
all the permalinks in Gengo::rewrite_rules (line 61 onwards) using
rewrite_rules_array because I can't see a way to add rewrite rules
before the first call to generate_rewrite_rules.  This works, but isn't
a good idea because it'll overwrite rewrite rules placed by other
plugins, I have to write a new rule for every view instead of making use
of the built-in ones, and also isn't futureproof.

What I'd like to have is a way of calling

$wp_rewrite->add_rewrite_tag('%language%', "(en|jp|fr)", 'language=');

which is on line 75 and adding %language% programmatically to the end of
each permalink structure *before* generate_rewrite_rules gets to work.

Somthing like this:

function generate_rewrite_rules($permalink_structure, $paged = true,
$feed = true, $forcomments = false, $walk_dirs = true) {

$this = & apply_filters('rewrite_codes', & $this);

$permalink_structure = apply_filters('permalink_structure',
$permalink_structure);

// Do Generating Stuff.

}

Then:

function my_rewrite_codes_function(& $rewrite_object)
{
$rewrite_object->add_rewrite_tag('%language%', "(en|jp|fr)", 'language=');
return $rewrite_object;
}
add_filter('rewrite_codes', 'my_rewrite_codes_function');

And:

function my_permalink_structure_function($structure)
{
return $structure . "%language%";
}
add_filter('permalink_structure', 'my_permalink_structure_function');


This would allow for far more flexible structures than we have now, if
I've figured it out right, at only the cost of two hooks.

There's one more thing, though.  Because of the way date rewrite rules
are generated, I don't think it's possible to add custom entries to
anything other than onto the end of the last section:

ie: /%year%/%month%/%day%/%language%/ can be generated, but
/%year%/%month%/%language%/ and /%year%/%language%/ can't.

I get round this at the moment by repeatedly calling
generate_rewrite_rules with walk_dirs set as false (lines 76 - 86) , but
it would be nice to say 'Append my tag onto any possible view' and just
have it happen.  I think this could be achieved by seperating out the
date generation logic, but I haven't looked into it too closely yet.

Maybe instead of:

$date_rewrite =
$this->generate_rewrite_rules($this->get_date_permastruct());
$date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite);

We could have:

$number_of_tokens = preg_match_all('/%.+?%/',
$this->get_date_permastruct(), $tokens);
$tokens = $tokens[0];
$date_rewrite = array();
while ($number_of_tokens--)
{
$permalink_structure = '/';
for ($i = 0; $i <= $number_of_tokens; $i++) $permalink_structure .=
$tokens[$i] . '/';
$date_rewrite += $this->generate_rewrite_rules($permalink_structure .
'%language%', true, true, false, false);
$date_rewrite += $this->generate_rewrite_rules($permalink_structure,
true, true, false, false);
}				
$date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite);

Is this feasible at all?  I'd be happy to create a patch with all of
this if it could be added.  I have a working version of the plugin
without any of these alterations, but compared to the rest of the Plugin
API (which I have to say is really fantastic compared with when I
started) it feels hacky.  Would be interested to hear what people thought.

Cheers,

Jamie.

By the way, this file will only work with the latest revisions as it
makes use of the new dbx hooks from the other day.

Oh yeah, and Ryan, that revision to stop the overwriting of $request
seems to be working well, thanks!

- --
http://jamietalbot.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDxsMcrovxfShShFARAg6QAJ4y2p5Xd6BRodVrjRXe3num1TU1+ACfZFL8
whHfJH7iCVakcl74Wh9qTBE=
=QWUv
-----END PGP SIGNATURE-----


More information about the wp-hackers mailing list