[wp-trac] [WordPress Trac] #54466: Rewrite rules flushed if we save options-permalink even without modification

WordPress Trac noreply at wordpress.org
Thu Nov 18 15:40:18 UTC 2021


#54466: Rewrite rules flushed if we save options-permalink even without
modification
----------------------------+-----------------------------
 Reporter:  nilav18         |      Owner:  (none)
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  Rewrite Rules   |    Version:  5.8.2
 Severity:  normal          |   Keywords:
  Focuses:  administration  |
----------------------------+-----------------------------
 Hi,

 I have custom rewrite rules applied with add_rewrite_rule(). To avoid
 flush rules each time, they are in a specific file that is only loaded if
 its modified date has changed.

 But, if I save permalink structure options in admin (/wp-admin/options-
 permalink.php), my rules are removed because the modified date of my file
 don't change. So, I call my file permalink_structure_changed and it's ok.

 Except for one case. If I don't change anything, just submit the form in
 options-permalink.php. The rewrite rules are flushed but
 permalink_structure_changed is fired only if we have a modification. Same
 thing with update_option_{$option}. And my custom rewrite rules are
 missed.

 So, I made a crappy patch who checks if we are on the options-
 permalink.php and load my custom rewrite rules file even if we don't
 submit the form. But, normally, I think if we have no changes, WordPress
 don't need to flush.

 Sample:
 {{{
 function custom_add_rewrite_rules() {
         add_rewrite_rule( '/([^/]*)/([^/]*)/?',
 'index.php?post_type=page&pagename=$matches[1]&query1=$matches[2]', 'top'
 );

         //Save the last modified date of the file in a Wordpress option
         update_option( 'custom_rewrite_rules_date', filemtime( __FILE__ )
 );
         flush_rewrite_rules();
 }

 //Compare the current modified date of the file with the last known
 modified date
 if ( filemtime( __FILE__ ) > get_option( 'pv_rewrite_rules_date' ) ) {
         add_filter( 'init', 'pv_add_rewrite_rules' );
 }

 /**
  * When we save the permalink structure in admin, custom rewrite rules are
 removed and to be reloaded.
  * Even if the permalink structure doesn't change.
  * There is no hook for that, just one in a real update case. @see
 permalink_structure_changed
  */
 if(is_admin()) {
         global $pagenow;
         if($pagenow === 'options-permalink.php') {
                 custom_add_rewrite_rules();
         }
 }

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/54466>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list