I'm working on some plugin where I need to enable custom URL rewrites. I want to enable editing of custom URL through Settings API, so basically I did everything that's needed to validate and update options. Rewrite URL option is already passed on plugin activation when registering custom post type, so I need to change it somehow after option update. The main problem which exists is how to flush_rewrite_rules() after validating that the value of rewrite URL was actually changed.
I register custom post type on init action and the validate which I'm writing about is set to register_setting inside admin_init action. Validate function runs once after changes in settings are saved and then the request is redirected back to options page.
I tried with the code below, but the URLs change only after second update (I must click save changes button twice):
function my_plugin_name_validate_options($args) {
foreach($args as $key => $value) {
switch ($key) {
case 'rewrite_url':
flush_rewrite_rules();
break;
case 'other':
// code for other cases... too long to post here
break;
}
}
}
This code is just an example, where I'm flushing rewrite rules, it's not actually used, but I'm validating each options field like that.
The question is where should I run flush_rewrite_rules() to update changes after validation/update of options?