Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Rewrite API/flush rules


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.


$wp_rewrite->flush_rules( $hard );


(boolean) (optional) Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush).
Default: true

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!


//Ensure the $wp_rewrite global is loaded
global $wp_rewrite;
//Call flush_rules() as a method of the $wp_rewrite object
$wp_rewrite->flush_rules( false );