WP_Rewrite::flush_rules() WordPress Method
The WP_Rewrite::flush_rules() method is used to refresh the rewrite rules used by WordPress. This is useful when you've made changes to your rewrite rules and need to update WordPress to reflect those changes.
WP_Rewrite::flush_rules( bool $hard = true ) #
Removes rewrite rules and then recreate rewrite rules.
Description
Calls WP_Rewrite::wp_rewrite_rules() after removing the ‘rewrite_rules’ option. If the function named ‘save_mod_rewrite_rules’ exists, it will be called.
Parameters
- $hard
(bool)(Optional)Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).
Default value: true
More Information
This method can be used to refresh WordPress’ rewrite rule cache. Generally, this should be used after programmatically adding one or more custom rewrite rules.
Because this function can be extremely costly in terms of performance, it should be used as sparingly as possible – such as during activation or deactivation of plugins or themes. Every attempt should be made to avoid using it in hooks that execute on each page load, such as init.
What it does
WordPress keeps a cache of all custom rewrite rules. Sometimes plugins or themes make modifications to those rules, however WordPress will not actually recognize the changes until the cache is regenerated.
This is not a procedural function, but a non-static method of the WP_Rewrite class. To call flush_rules(), you must first ensure you are using WordPress’ $wp_rewrite global, and call it as a method (see “Usage” above for an example).
Note: This same method is called whenever permalink settings are changed or saved in the WordPress admin, so rewrite rules can be manually refreshed by visiting the Settings > Permalinks screen in WordPress’s admin.
WARNING: If this function is called without a parameter or with a parameter of true, your .htaccess will be overwritten and any custom rules will be lost!
Source
File: wp-includes/class-wp-rewrite.php
public function flush_rules( $hard = true ) { static $do_hard_later = null; // Prevent this action from running before everyone has registered their rewrites. if ( ! did_action( 'wp_loaded' ) ) { add_action( 'wp_loaded', array( $this, 'flush_rules' ) ); $do_hard_later = ( isset( $do_hard_later ) ) ? $do_hard_later || $hard : $hard; return; } if ( isset( $do_hard_later ) ) { $hard = $do_hard_later; unset( $do_hard_later ); } update_option( 'rewrite_rules', '' ); $this->wp_rewrite_rules(); /** * Filters whether a "hard" rewrite rule flush should be performed when requested. * * A "hard" flush updates .htaccess (Apache) or web.config (IIS). * * @since 3.7.0 * * @param bool $hard Whether to flush rewrite rules "hard". Default true. */ if ( ! $hard || ! apply_filters( 'flush_rewrite_rules_hard', true ) ) { return; } if ( function_exists( 'save_mod_rewrite_rules' ) ) { save_mod_rewrite_rules(); } if ( function_exists( 'iis7_save_url_rewrite_rules' ) ) { iis7_save_url_rewrite_rules(); } }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
2.0.1 | Introduced. |