0

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?

1 Answer 1

3

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.

This is the key to your issue. You're registering the custom post type, and presumably you're setting up the rewrite rules in that registration.

When you call flush_rewrite_rules(), the rules are rebuilt right then, at that time. So, in your validation function, you have not saved the new setting for your rewrite rules yet, nor have you updated the custom post type registration with the new rule.

Note: A sanitization/validation function callback added using register_setting runs before settings are saved to the database, not after.

Basically, you need to handle the change better. You can do this in the validation function, but you may need to modify the already registered CPT first. Basically, register it again with the changes. Then, when you flush the rewrite rules, it should rebuild them correctly, with your modifications.

The reason this works for you on the second attempt is because of the way you're saving and flushing. You flush the first time, but nothing changes in the rewrite rules. Then, your option gets saved. On the next load, the CPT is registered with your new information, so your new rule is there, but you haven't flushed yet. Saving again causes a second flush, and now your rule takes effect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.