Forums

[resolved] Adding rewrite rules and $wp_rewrite->flush_rules() (7 posts)

  1. microkid
    Member
    Posted 2 months ago #

    Hi,

    I've been struggling with this for a while, and the documentation on the rewrite API is a little scarce. So I hope someone's out there who can shed some light on this.

    If I add rewrite rules, do I have to flush the rules on every page load? It seems that if I don't flush them, it will keep working for a short while. Then after a couple of minutes it will break.

    Flushing the rules currently causes an extra 100 (!) mysql queries on each page load. The queries look like this:

    [23] => Array
            (
                [0] => SELECT * FROM wp_posts WHERE ID = 5106 LIMIT 1
                [1] => 0.000135898590088
                [2] => require, wp, WP->main, do_action_ref_array, call_user_func_array, XIC_flush, WP_Rewrite->flush_rules, WP_Rewrite->wp_rewrite_rules, WP_Rewrite->rewrite_rules, WP_Rewrite->page_rewrite_rules, WP_Rewrite->page_uri_index, get_page_uri, get_page, get_post
            )
    
        [24] => Array
            (
                [0] => SELECT <code>post_parent</code> FROM wp_posts WHERE ID = 5068 LIMIT 1
                [1] => 0.0001220703125
                [2] => require, wp, WP->main, do_action_ref_array, call_user_func_array, XIC_flush, WP_Rewrite->flush_rules, WP_Rewrite->wp_rewrite_rules, WP_Rewrite->rewrite_rules, WP_Rewrite->page_rewrite_rules, WP_Rewrite->page_uri_index, get_page_uri, get_page, get_post, _get_post_ancestors
            )
    
        [25] => Array
            (
                [0] => SELECT ID, post_name, post_parent FROM wp_posts WHERE post_type = 'attachment' AND post_parent = 5106
                [1] => 0.000179052352905
                [2] => require, wp, WP->main, do_action_ref_array, call_user_func_array, XIC_flush, WP_Rewrite->flush_rules, WP_Rewrite->wp_rewrite_rules, WP_Rewrite->rewrite_rules, WP_Rewrite->page_rewrite_rules, WP_Rewrite->page_uri_index
            )

    They are "small" queries, but still I'd love to get rid of them..

  2. microkid
    Member
    Posted 2 months ago #

    *bump*

  3. terryjsmith
    Member
    Posted 1 month ago #

    While I do not have a solution yet, I have spent all day (and the last week) trying to figure out the same issue with one of the Wordpress core developers.

    Our issue at least is in using the object cache. We have set ours up for a default timeout of 5 minutes, which is the same amount of time the URLs seemed to be "good" for.

    What happening is that there is no hook available for when WP is generating the rewrite rules. This is happening before the 'init' action and at most other actions, the $wp_rewrite variable has not been initialized yet (even if you initialize it, it generates the rules right away).

    The developer I spoke with is looking into adding another hook into the code, or possibly getting rid of the rewrite rules being transient (as of 2.8) all together.

    I hope this helps; I am still searching for a temporary fix and will let you know if I find one.

    Terry

  4. microkid
    Member
    Posted 1 month ago #

    Actually, I did come up with a solution.

    There's a filter called when the rewrite rules are created: rewrite_rules_array

    Using this seems to work and does NOT time out:

    function my_rewrite_rules( $rewrite_rules ) {
    
        $new_rules = array( 'foobar/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?foobar=$matches[1]&paged=$matches[2]' );
    
        $rewrite_rules = $new_rules + $rewrite_rules;
    
        return $rewrite_rules;
    
    }
    
    add_filter('rewrite_rules_array', 'my_rewrite_rules');

    Note that you will need to flush the rewrite rules once. This can easily be done by going to the options/permalinks page in your wp-admin.

    Sorry for not letting you know earlier. Please let me know if this will work for you as well.

  5. terryjsmith
    Member
    Posted 1 month ago #

    Wow, nicely done. Unfortunately I was trying to add an endpoint so the rewrite rules become several degrees more complicated. The functionality is actually being reversed in Wordpress so the original implementation will soon work as well. We're testing a patch on the WP core.

    Thanks for posting a solution though, that's awesome that you found a workaround :)

    Terry

  6. markjaquith
    Moderator
    Posted 1 month ago #

    This is a bug in the way we store rewrite_rules in "transients" instead of options like we used to. I think our fix for WP 2.9 is going to be to revert to the old method.

    http://core.trac.wordpress.org/ticket/10981

    In more detail, people using transient object cache backends like Memcache or APC can expect their rewrite_rules transient to... well... transit (go away). When this happens, WP dutifully regenerates the rules, but it does so before any plugin would have added its endpoint (and indeed there wasn't an appropriate hook for the plugin to add it such that WP would be aware of it in this case). So the rules get saved without your endpoint changes. This will be fixed for WP 2.9!

  7. microkid
    Member
    Posted 1 month ago #

    That's amazing, I've been struggling with this for over a year!

    Please let me know if you need me for testing, I've been using custom rewrites a lot.

    Thanks guys.

Reply

You must log in to post.

About this Topic