[wp-trac] [WordPress Trac] #18450: New safe action to add rewrite rules on

WordPress Trac noreply at wordpress.org
Fri Mar 14 20:10:02 UTC 2014


#18450: New safe action to add rewrite rules on
-------------------------------------------------+-------------------------
 Reporter:  simonwheatley                        |       Owner:
     Type:  enhancement                          |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
Component:  Rewrite Rules                        |  Review
 Severity:  normal                               |     Version:  3.2.1
 Keywords:  has-patch dev-feedback needs-        |  Resolution:
  refresh                                        |     Focuses:
-------------------------------------------------+-------------------------

Comment (by tott):

 Whenever I ran into this problem so far I'd either enforce rules using
 pre_option_rewrite_rules or if I like to keep things as dynamic as
 possible I use some code along the lines of the following code hooked to
 'init' to ensure that my rewrite rules will be flushed when needed but not
 on every page load.

 {{{
         function maybe_flush_rules() {
                 global $wp_rewrite;
                 $rewrite_rules = get_option( 'rewrite_rules' );
                 foreach( $rewrite_rules as $rule => $rewrite ) {
                         $rewrite_rules_array[$rule]['rewrite'] = $rewrite;
                 }
                 $maybe_missing = $wp_rewrite->rewrite_rules();
                 $missing_rules = false;
                 $rewrite_rules_array = array_reverse(
 $rewrite_rules_array, true );
                 foreach( $maybe_missing as $rule => $rewrite ) {
                         if ( ! array_key_exists( $rule,
 $rewrite_rules_array ) ) {
                                 $missing_rules = true;
                                 break;
                         }
                 }
                 if ( true === $missing_rules ) {
                         flush_rewrite_rules();
                 }
         }
 }}}

 This checks only for the existance of the key in the rules but serves me
 well in most cases and could be extended to also catch updates in rules.

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


More information about the wp-trac mailing list