[wp-trac] [WordPress Trac] #29118: Registering rewrite rules is hard, so let's introduce remove_rewrite_rule.

WordPress Trac noreply at wordpress.org
Sun Sep 7 21:17:39 UTC 2014


#29118: Registering rewrite rules is hard, so let's introduce remove_rewrite_rule.
---------------------------+------------------------------
 Reporter:  ericlewis      |       Owner:
     Type:  enhancement    |      Status:  new
 Priority:  normal         |   Milestone:  Awaiting Review
Component:  Rewrite Rules  |     Version:
 Severity:  normal         |  Resolution:
 Keywords:  has-patch      |     Focuses:
---------------------------+------------------------------

Comment (by mboynes):

 I really dig this idea, and that race condition has bugged me for longer
 than I'd care to say. My workaround for that has always been, on
 activation or deactivation, just delete the `rewrite_rules` option -- this
 will cause them to be regenerated in the next request. Not ideal.

 My main concern with your patch is that it pretty much requires
 repetition. If I call `add_rewrite_rule()` 20 times, that also means I'd
 have to call `remove_rewrite_rule()` 20 times with the same arguments.
 There would be ways around this, e.g. using `call_user_func_array()`, but
 I think any solution would be a bit much for the average developer.
 Besides keeping code DRY, having to repeat rules is asking for trouble. It
 would be very simple for someone to add a rule to their "add"
 function/method, but forget to add it to the "remove" counterpart.

 I'm not sure of the best route on this. The first idea that popped to mind
 is to add another argument (ugh) to `add_rewrite_rule()` indicating the
 source/name/group. For instance:

 {{{
 add_rewrite_rule( 'bar/?$', 'index.php?foo=bar', 'top', 'my-plugin' );
 add_rewrite_rule( 'bar/(\d+)/?$', 'index.php?foo=bar&bat=$matches[1]',
 'top', 'my-plugin' );
 add_rewrite_rule( 'bar.json$', 'index.php?foo=bar&format=json', 'top',
 'my-plugin' );
 }}}

 This opens up the possibility to then call something like
 `remove_rewrite_rules( 'my-plugin' );` and remove them all. As an added
 bonus, we could then filter added rewrite rules as we can with
 `add_permastruct()`, e.g. `add_filter( 'my-plugin_rewrite_rules',
 '__return_empty_array' )`.

 Regardless of the solution, this is a great initiative. Thanks for getting
 it started!

--
Ticket URL: <https://core.trac.wordpress.org/ticket/29118#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list