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

WordPress Trac noreply at wordpress.org
Sun Dec 19 16:37:58 UTC 2021


#54466: Rewrite rules flushed if we save options-permalink even without
modification
---------------------------+-----------------------------
 Reporter:  nilav18        |       Owner:  (none)
     Type:  defect (bug)   |      Status:  closed
 Priority:  normal         |   Milestone:
Component:  Rewrite Rules  |     Version:  5.8.2
 Severity:  normal         |  Resolution:  duplicate
 Keywords:                 |     Focuses:  administration
---------------------------+-----------------------------
Changes (by SergeyBiryukov):

 * resolution:  invalid => duplicate
 * milestone:  Awaiting Review =>


Old description:

> 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();
>         }
> }
>
> }}}

New description:

 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();
         }
 }

 }}}

--

Comment:

 Hi there, welcome back to WordPress Trac! Thanks for the ticket.

 We're already tracking this issue in #40440, let's continue the discussion
 there.

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


More information about the wp-trac mailing list