[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, '/') .
    $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:

    string(32) "index.php?attachment=$matches[1]"
    string(37) "index.php?attachment=$matches[1]&tb=1"
    string(49) "index.php?attachment=$matches[1]&feed=$matches[2]"
    string(49) "index.php?attachment=$matches[1]&feed=$matches[2]"
    string(32) "index.php?attachment=$matches[1]"
    string(37) "index.php?attachment=$matches[1]&tb=1"
    string(49) "index.php?attachment=$matches[1]&feed=$matches[2]"
    string(49) "index.php?attachment=$matches[1]&feed=$matches[2]"

The suggestions made here
make a lot of sense, but that was several months ago; are there plans
to sane-ify wp_rewrite for 2.1?



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