Flush rewrite rules when it's necessary. This could be put in an init hook or the like and ensures that the rewrite rules option is only rewritten when the generated rules don't match up with the option.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment