2

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".

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();
    }
0

Check the code in wp-admin/options-permalink.php lines 91 to 129 (3.0.5). Compare if you do all that.

  • Looks like I'm doing everything that's there. Can you see anything that's missing? – Scott B Feb 21 '11 at 17:52
  • @Scott B: Well what i saw in there was that it seemed to do some prep operations on the permalink structure? – wyrfel Feb 22 '11 at 16:03
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);
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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