0

Flushing rules is clearly an important part of creating themes with custom post types. See here and here.

Does anyone have any example code of how to flush rules from functions.php?

I'm a little surprised this isn't covered in the custom post type pages of the Codex.

Update: I tried adding this to functions.php, but it didn't work:

register_deactivation_hook( __FILE__, array(&$this,'deactivate' ) );
function deactivate() {
 global $wp_rewrite;
 $wp_rewrite->flush_rules();
}
0

5 Answers 5

4

While the solutions provided here do still work, WordPress has evolved since and does now (since 3.3, I believe) provide direct hooks for theme activation.

after_switch_theme will fire on theme activation and switch_theme before deactivating an old theme.

Hence the up-to-date answer is:

function reflush_rules() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
add_action( 'after_switch_theme', 'reflush_rules' );
2

this code (taken from Ozh's comment here with small addition) may help you.

function reflush_rules() {
  if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
  }
}
add_action('init', 'reflush_rules');

edit:

add this function on your functions.php. This function will only loaded when theme activated (the only time $_GET['activated'] is set).

2
  • Nope, that didn't work.
    – jnthnclrk
    Commented Oct 21, 2010 at 14:51
  • Need to define $pagenow first. Added a new answer which does that.
    – jnthnclrk
    Commented Oct 22, 2010 at 12:54
1

Not sure about flushing on deactivation, but activation is pretty easy.

In your functions.php file, set up some code like the following:

function flush_rules_on_activation() {  
    global $wp_rewrite;
    $is_installed = get_option('theme_installed');
    if(!$is_installed) {
        $wp_rewrite->flush_rules();
        add_option('theme_installed', true);
    }
}
add_action('init', 'flush_rules_on_activation');

This will run every time, but the rules will only be flushed once because you set a flag in your options table to prevent flushing them every time.

4
  • That code killed the whole page.
    – jnthnclrk
    Commented Oct 21, 2010 at 14:58
  • Just tested it on my own site ... it didn't kill anything.
    – EAMann
    Commented Oct 21, 2010 at 15:09
  • Probably an error on my side. Although I prefer the Ozh solution.
    – jnthnclrk
    Commented Oct 21, 2010 at 19:37
  • I prefer Ozh's solution as well ... the only reason I posted this was in response to your "Nope, that didn't work" comment.
    – EAMann
    Commented Oct 21, 2010 at 20:15
1

I had to modify bangbambang's answer to get this to work.

The code should be:

add_action('init', 'reflush_rules');
function reflush_rules() {
$pagenow = $_SERVER['SCRIPT_NAME'];
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "/wp-admin/themes.php" ) {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
}
0

What actually seem to work is adding this line after register_post_type:

flush_rewrite_rules( false );
2
  • This works, but it is inefficient, so you should not call it on every init.
    – Jan Fabry
    Commented Oct 21, 2010 at 18:55
  • OK, deleted in favour of an init hook. Don't really understand why, but taking your advice anyway.
    – jnthnclrk
    Commented Oct 21, 2010 at 19:38

Your Answer

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

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