flush_rewrite_rules( bool $hard = true )
Remove rewrite rules and then recreate rewrite rules.
Parameters 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 More Information
This function is useful when used with custom post types as it allows for automatic flushing of the WordPress rewrite rules (usually needs to be done manually for new custom post types). However, this is an expensive operation so it should only be used when necessary.
Source Source
File: wp-includes/rewrite.php
function flush_rewrite_rules( $hard = true ) { global $wp_rewrite; if ( is_callable( array( $wp_rewrite, 'flush_rules' ) ) ) { $wp_rewrite->flush_rules( $hard ); } }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
3.0.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Expand full source codeCollapse full source code
If you want to flush rules while updating posts based on post type:
Expand full source codeCollapse full source code
This is how you would flush rules on theme activation:
If you’re developing a theme, while building it you can use this snippet of code that will flush rewrite rules when the file containing it is changed, or every 48 hours:
Expand full source codeCollapse full source code
Feedback
“Flushing” the rewrite rules should only happen in wp-admin, see https://core.trac.wordpress.org/ticket/44142. It is possible to do from the front-end while developing a theme or a plugin, but is a pretty bad idea if ever done in production.In that terms I’d add:
if ( ! is_admin() ) { return; }
at the top of the above function. — By Andrew Ozz —A relatively simple way to flush rewrite rules on activation and deactivation hooks is not using flush_rewrite_rules() at all. Instead just clear the rewrite_rules option to force WordPress to recreate them at the right time.
Example:
This avoids having to do complicated stuff like registering your custom post types in the activation hook or throwing and catching dedicated db option or transients as suggested in other notes…
This is how you would flush rewrite rules when a plugin is activated or deactivated:
Expand full source codeCollapse full source code
Expand full source codeCollapse full source code