Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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();
}
share|improve this question
add comment

5 Answers

up vote 1 down vote accepted

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' );
share|improve this answer
add comment

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

share|improve this answer
    
Nope, that didn't work. –  jnthnclrk Oct 21 '10 at 14:51
    
Need to define $pagenow first. Added a new answer which does that. –  jnthnclrk Oct 22 '10 at 12:54
add comment

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.

share|improve this answer
    
That code killed the whole page. –  jnthnclrk Oct 21 '10 at 14:58
    
Just tested it on my own site ... it didn't kill anything. –  EAMann Oct 21 '10 at 15:09
    
Probably an error on my side. Although I prefer the Ozh solution. –  jnthnclrk Oct 21 '10 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 Oct 21 '10 at 20:15
add comment

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();
}
}
share|improve this answer
add comment

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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