Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#37892 closed defect (bug) (fixed)

Make sure rewrite rules are written once 'wp_loaded' is fired

Reported by: Chouby Owned by: SergeyBiryukov
Milestone: 4.7 Priority: normal
Severity: normal Version:
Component: Rewrite Rules Keywords: has-patch
Focuses: Cc:


With #30501, it was made sure that an early call to flush_rewrite_rules() would not break rewrite rules.

However, in the context of network activation / deactivation, some plugins don't flush the rewrite rules with flush_rewrite_rules() but with a direct call to delete_option( 'rewrite_rules' );. @jeremyfelt explained the reason for doing this in https://jeremyfelt.com/2015/07/17/flushing-rewrite-rules-in-wordpress-multisite-for-fun-and-profit/. For example, Yoast SEO and Polylang are flushing rules at activation / deactivation using this way.

Now, if another plugin uses the rewrite rules before the wp_loaded action is fired (for example by calling url_to_post_id(), the rewrite rules are saved too early and are broken.

Plugins could fix this at activation, but obviously not at deactivation.

Inspired by what has been done to fix #30501, I propose to flush the rewrite rules in the 'wp_loaded' action if there is an attempt to save them before this action has been fired.

Attachments (1)

37892.patch (508 bytes) - added by Chouby 4 years ago.

Download all attachments as: .zip

Change History (6)

4 years ago

#1 @Chouby
4 years ago

  • Keywords has-patch added

#2 @SergeyBiryukov
4 years ago

  • Milestone changed from Awaiting Review to 4.7
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#3 @jorbin
4 years ago

@SergeyBiryukov What do you think of the patch?

This ticket was mentioned in Slack in #core by stevenkword. View the logs.

4 years ago

#5 @jorbin
4 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 38751:

Make sure rewrite rules are not written until wp_loaded has fired

If a plugin attempts to change the rewrite rules to early, other plugins may have their rules inadvertently discarded. Additionally, some function such as url_to_post_id cause a rewrite rule lookup that could cause this accidental flushing. This forces the flushing to only occur once wp_loaded has been fired.

Fixes #37892.
Props Chouby.

Note: See TracTickets for help on using tickets.