WordPress.org

Make WordPress Core

Opened 2 months ago

Closed 4 weeks ago

Last modified 4 weeks ago

#54466 closed defect (bug) (duplicate)

Rewrite rules flushed if we save options-permalink even without modification

Reported by: nilav18 Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.8.2
Component: Rewrite Rules Keywords:
Focuses: administration Cc:

Description (last modified by SergeyBiryukov)

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

Change History (2)

#1 @bradleyt
4 weeks ago

  • Resolution set to invalid
  • Status changed from new to closed

Hi Nilav18, Thanks for your ticket.

The rewrite rules are actually flushed every time the options-permalink.php page is visited, even if the options are not saved. This happens with a call to flush_rewrite_rules(); at the top of the options-permalink.php file. The permalinks have been flushed in this way for at least 10 years, and there are many people who now know to expect and rely on this method for flushing the permalinks.

Usual best practice is to make calls add_rewrite_rule on every page load (i.e. within the init action), so that the rules exist whenever flush_rewrite_rules is called. As a plugin or theme author you'd be expected to rarely call flush_rewrite_rules(), maybe only as part of the initial plugin activation. You may also need to re-run flush_rewrite_rules as part of an upgrade sequence if your rewrite rules change. You may be able to get extra help with the correct usage of the WordPress rewrite rules API on the support forums.

Whilst I can see how the behaviour can initially be confusing, I don't there there is an actual bug here and so I am going to close this ticket.

#2 @SergeyBiryukov
4 weeks ago

  • Description modified (diff)
  • Milestone Awaiting Review deleted
  • Resolution changed from invalid to duplicate

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.

Note: See TracTickets for help on using tickets.