WP_Rewrite::flush_rules()publicWP 2.0.1

Updates the permalink (SEO-friendly URL) rewrite rules in the database and cache.

See the wrapper function flush_rewrite_rules().

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.

Method of the class: WP_Rewrite{}

Hooks from the method

Returns

null. The function returns nothing.

Usage

$wp_rewrite->flush_rules();

Examples

0

#1 Example of updating rewrite rules of URLs:

// make sure the $wp_rewrite variable is defined globally
global $wp_rewrite;

$wp_rewrite->flush_rules();

Or you could do it this way:

$GLOBALS['wp_rewrite']->flush_rules();
0

#2 Example of updating rewrite rules using the function flush_rewrite_rules(), while the plugin is activated:

register_activation_hook( __FILE__, 'author_base_rewrite' );

function author_base_rewrite(){
	flush_rewrite_rules( false );
}

flush_rewrite_rules( false ) - false indicates that the file .htaccess should not be updated, if false is not specified, the file .htaccess will be updated according to the rules.

Changelog

Since 2.0.1 Introduced.

WP_Rewrite::flush_rules() code WP 6.9

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 );
	}

	$this->refresh_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();
	}
}