[wp-hackers] generate_rewrite_rule misbehaving

Mike Purvis mike at uwmike.com
Fri Jul 7 15:18:31 GMT 2006


Context: I'm making a simple plugin to aid in inserting mini-galleries
into posts. I realise that there are lots of other plugins that do
this same basic task, but one of the features I'd like to have is
"click the thumbnail to bring up mid-view in blog template". So if the
post is <http://example.com/blog/2006/07/07/my-post/>, then you could
view one of the pics at
<http://example.com/blog/2006/07/07/my-post/photos/hpim4523/>, and
then click it again to see the massive-sized original.

I know WP2 has some built-in features for attached images and so-on,
but I've got Photon compiled as a Universal now and working with
iPhoto 6, so I'd prefer to use that as the uploading tool.

Whatever. Here's what I've got. (which does work perfectly, it just
generates a boatload of extraneous rules... why would I have a feed
for a single image within a post?)

function wp_quickgal_rewrite_rules($wp_rewrite) {
    global $wp_rewrite;

    // add rewrite tokens
    $photo_tag = '%photoname%';
    $wp_rewrite->add_rewrite_tag($photo_tag, '([^/]+)', 'photoname=');

    $structure = rtrim($wp_rewrite->permalink_structure, '/') .
"/photos/{$photo_tag}/";
    $rules = $wp_rewrite->generate_rewrite_rule($structure);
    $wp_rewrite->rules += $rules;

    //var_dump($wp_rewrite); die();
}

add_filter('generate_rewrite_rules', 'wp_quickgal_rewrite_rules');

If I uncomment the var_dump line and then re-save the rules, the tail
end of the "rules" array looks like this:

    ["articles/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/photos/([^/]+)/trackback/?$"]=>
    string(107)
"index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&photoname=$matches[5]&tb=1"
    ["articles/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/photos/([^/]+)(/[0-9]+)?/?$"]=>
    string(119)
"index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&photoname=$matches[5]&page=$matches[6]"
    ["articles/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/photos/[^/]+/([^/]+)/?$"]=>
    string(32) "index.php?attachment=$matches[1]"
    ["articles/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/photos/[^/]+/([^/]+)/trackback/?$"]=>
    string(37) "index.php?attachment=$matches[1]&tb=1"
    ["articles/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/photos/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=>
    string(49) "index.php?attachment=$matches[1]&feed=$matches[2]"
    ["articles/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/photos/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=>
    string(49) "index.php?attachment=$matches[1]&feed=$matches[2]"
    ["articles/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/photos/[^/]+/attachment/([^/]+)/?$"]=>
    string(32) "index.php?attachment=$matches[1]"
    ["articles/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/photos/[^/]+/attachment/([^/]+)/trackback/?$"]=>
    string(37) "index.php?attachment=$matches[1]&tb=1"
    ["articles/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/photos/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=>
    string(49) "index.php?attachment=$matches[1]&feed=$matches[2]"
    ["articles/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[^/]+/photos/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=>
    string(49) "index.php?attachment=$matches[1]&feed=$matches[2]"

The suggestions made here
<http://comox.textdrive.com/pipermail/wp-trac/2006-February/000109.html>
make a lot of sense, but that was several months ago; are there plans
to sane-ify wp_rewrite for 2.1?

Thanks,

Mike



On 7/7/06, brian at thecodecave.com <brian at thecodecave.com> wrote:
> >Hi,
> >
> >it seems I'm still getting around 10 rules,
> >corresponding to trackbacks, multiple feeds, and attachments.
>
> Without seeing exactly what you are, I need to ask, are you seeing the results of wp-includes\classes.php's function rewrite_rules()?
>
> That returns an array of the rules (8 by default) needed to process all the ways your blog is accessed.  For instance, a special rule is needed when you click on a category, one is needed for links to pages, and etc.  I suppose pieces could be eliminated if, for instance, you didn't have a calendar, but the benifit would be negligable....
>
> If that's not what you're looking at, an example might help...
>
> _______________________________________________
> Brian Layman
> www.TheCodeCave.com
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list