flush_rewrite_rules()WP 3.0.0

Updates the permalink rewrite rules (pretty URLs) in the database and cache. Deletes existing rules, generates and writes new ones.

Must be used after adding new URL rewrite rules for the new rules to take effect; otherwise, they will not work. New rules are added using the function add_rewrite_rule(), or when registering a post type or taxonomy.

WordPress saves all rewrite rules in the option rewrite_rules (in the cache). Sometimes plugins, themes, or functions may add their own new rewrite rules, and WordPress will not know about these new rules until all rules are rewritten (updated).

This function is resource-intensive, so it should not be called every time a page is generated (on request). It needs to be used only once! For example, during the activation/deactivation of a plugin/theme, or after a condition (check) that it needs to be done.

It is recommended to reset the rewrite rules no earlier than the action wp_loaded.

In general, the function's code includes a check - this function reassigns its own call if it is called before the action wp_loaded. However, sometimes such reassignment can cause bugs, so if possible, it is better to call the function during the action wp_loaded immediately.

If you created a new post type using the function register_post_type(), then created a new post of that type, and upon visiting it saw a 404 error, it means the pretty URL rewrite rules were not reset.

This happens because new rewrite rules (stored in the database) were not created. This function is needed to create them.

Other options for resetting rewrite rules:

You can manually reset the pretty URL rewrite rules through the "Permalinks" settings page. When visiting this page, the rewrite rules are deleted from the database, new ones are generated, and saved in place of the old ones.

In older versions of WP, you also had to click the Save button on this page.

Also, to reset, you can empty the option rewrite_rules (which stores the pretty URL rules).

update_option( 'rewrite_rules', '' );

On the next page generation, the option will be created automatically. See wp_rewrite_rules(). At the same time, the basic pretty URL rules will be obtained immediately as needed, and an action will be created for regeneration on the hook wp_loaded, to correctly update the pretty URLs once again.

1 time — 0.010174 sec (extremely slow) | 50000 times — 196.39 sec (extremely slow) | PHP 7.1.11, WP 4.9.8

No Hooks.

Returns

null. Returns nothing.

Usage

flush_rewrite_rules( $hard );
$hard(boolean)
Whether to update the .htaccess file or just update the rules. By default - true, the .htaccess file is updated.
Default: true

Examples

0

#1 Refresh Friendly URL rules on plugin activation/deactivation

Example, shows how to reset rewrite rules Friendly URL at the moment of plugin activation/deactivation:

register_activation_hook( __FILE__, 'myplugin_activate' );
register_deactivation_hook( __FILE__, 'myplugin_deactivate' );

function myplugin_activate() {

	// Here, we do what we need to do when activating the plugin

	flush_rewrite_rules();
}

function myplugin_deactivate() {
	flush_rewrite_rules();
}
0

#2 Flush Rules on theme activation/deactivation

This example shows how to reset the Friendly URL rewrite rules when the theme is activated:

/* Reset rewrite rules. */
add_action( 'after_switch_theme', 'bt_flush_rewrite_rules' );

function bt_flush_rewrite_rules() {
	 flush_rewrite_rules();
}
0

#3 Reset Friendly URL if file has been changed or every 48 hours

If you're developing a theme, a code that will reset the Friendly URL rewrite rules if the file has been changed or every 48 hours might be useful while you're developing it.

// do not use on live/production servers
add_action( 'init','maybe_rewrite_rules' );

function maybe_rewrite_rules() {

	// do it in admin only
	if( ! is_admin() ){
		return;
	}

	// Get the file time as the version number
	$ver = filemtime( __FILE__ );
	$defaults = [ 'version' => 0, 'time' => time() ];
	$r = wp_parse_args( get_option( __CLASS__ . '_flush', [] ), $defaults );

	// reset if the version has changed if 48 hours have passed.
	if ( $r['version'] != $ver || $r['time'] + 172800 < time() ) {
		flush_rewrite_rules();

		$args = array( 'version' => $ver, 'time' => time() );
		if ( ! update_option( __CLASS__ . '_flush', $args ) )
			add_option( __CLASS__ . '_flush', $args );
	}

}

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

Notes

  • Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.

Changelog

Since 3.0.0 Introduced.

flush_rewrite_rules() code WP 6.8.3

function flush_rewrite_rules( $hard = true ) {
	global $wp_rewrite;

	if ( is_callable( array( $wp_rewrite, 'flush_rules' ) ) ) {
		$wp_rewrite->flush_rules( $hard );
	}
}