Long time listener, first time caller.
Large website has many years of WordPress content, many many taxonomies, categories and tags. In an effort to localize some of the URLs, we are doing something like the following:
add_action('init', function() {
...
add_filter( 'generate_rewrite_rules', 'add_country_lang_prefix_to_rewrite_rules', 99 );
...
}
This does a little work:
/**
* Add country lang prefix to rewrite rules
*/
function add_country_lang_prefix_to_rewrite_rules( $wp_rewrite ) {
$wp_rewrite->rules = add_country_codes_rewrite_rule($wp_rewrite->rules);
$wp_rewrite->extra_rules = add_country_codes_rewrite_rule($wp_rewrite->extra_rules);
$wp_rewrite->extra_rules_top = add_country_codes_rewrite_rule($wp_rewrite->extra_rules_top);
}
Nothing special:
add_country_codes_rewrite_rule
iterates through each rule and adds a new one with a localized url for a country we are adding support for. Theres nothing special about this function other than it’s creating a new rule for each existing rule, prefixing the a localized prefix (as an allowed destination / route). Only call out here is that we “allow” list certain patterns, excluding json urls and internal urls to wordpress that should not be localized.
For us, it seems like generate_rewrite_rules
is being fired on every admin page load as we can see an up-sert / POST to the DB with a query updating the options table.
UPDATE `wp_options` SET `option_value` = "...." WHERE `option_name` = 'rewrite_rules'
The question is, when in the lifecycle is it appropriate to fire the rewrite rules? Since this updates a large blob into the db, we would like to only do it when it’s necessary. I see examples on this site that reference init
as being the right fire point for adding the filter, but this seems to lead to WP updating the rules on every load, and also every post update. Looking to see if this is a bug on our end.
We have tried various versions of whats documented, but mainly interested in best practice. I’m expecting rules to only update when absolutely necessary i.e. when we update a taxonomy, category, or the actual rules.
RG-79 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.