Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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.
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