$location) { // make sure it gets into the database if(!isset($rules[$regex]) || $force) { $rules[$regex] = $location; $changed = TRUE; } // make sure it shows up in the class as well, this is less important // we put it into the extra_rules as well to make sure it will live on // if we rewrite the rules. $wp_rewrite->rules[$regex] = $location; $wp_rewrite->extra_rules[$regex] = $location; } // update the db options if($changed || $force) { // discard any changed (and no longer useful) rules that no longer // need to be in the database $wp_rewrite->rewrite_rules(); krsort($wp_rewrite->rules); update_option('rewrite_rules', $wp_rewrite->rules); // if .htaccess doesn't exist for some reason, create it. $htaccess = WP_MAIN_DIR .'/.htaccess'; if(!file_exists($htaccess)) piggyback_force_rewrite($htaccess); } } /** * Force a rewrite of the .htaccess file if it doesn't yet exist. * @author Peter Goodman * @internal */ function piggyback_force_rewrite($htaccess_file) { global $wp_rewrite; $rules = $wp_rewrite->mod_rewrite_rules(); $fp = fopen($htaccess_file, "w"); // write the rules to the .htaccess if(is_resource($fp)) { fwrite($fp, $rules); fclose($fp); } } // could use add_action, but it just calls add_filter and so we will ignore // the distinction. The priority is 1 so that it is executed *before* the // redirect_canonical() function add_filter('template_redirect', 'piggyback_rewrite_rules', 1);