404 errors after plugin options update and category base change

I’m calling the function below after each “save changes” on my plugin’s settings file via the sanitization callback.

The plugin allows the end user to change their category base, and needs to completely rewrite the permalinks in order to avoid 404 errors. However, I’m still getting 404s. The only way I can resolve them is to go to the “Settings > Permalinks” manager and clicking “Save Changes”.

Read More

What am I missing in this function to get rid of the 404s?

function myplugin_refresh() {
    wp_cache_flush();
    global $wp_rewrite;
    $my_permalinks = get_option('permalink_structure');
    $wp_rewrite->set_permalink_structure($my_permalinks);
    $wp_rewrite->flush_rules();
    }

Related posts

Leave a Reply

2 comments

  1. wp_rewrite is not initialized in register_activation_hook()

    In your hook activation code add this line….

    add_option('reset_permalink_structure',true);
    

    In your plugin code add….

    add_action('admin_init',function(){
            if( get_option('reset_permalink_structure') ) {
                global $wp_rewrite;
                $my_permalinks = get_option('permalink_structure');
                $wp_rewrite->set_permalink_structure($my_permalinks);
                $wp_rewrite->flush_rules();
                add_option('reset_permalink_structure',false);
            }
        }