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

$hardbooloptional
Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).

Default: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

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

Hooks

apply_filters( ‘flush_rewrite_rules_hard’, bool $hard )

Filters whether a “hard” rewrite rule flush should be performed when requested.

Changelog

VersionDescription
2.0.1Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.