The Flush_rewrite_rules () function is used to remove and then override the pseudo static rule based on existing conditions, that is, to flush a pseudo static rule.
For the first moment, it is usually invoked when a topic or plug-in adds a new custom article type to prevent 404 of articles from new custom article types, or many times we need to execute some code when the theme is enabled, such as arranging some database forms, jumping to the settings page, and so on, WordPress itself does not provide related hooks, online also has a lot of various ways to achieve, after my research, found that may be the best way to share below:
/**
*wordpress executes some code when the theme is enabled
*http://www.endskin.com/theme-activation-action/
*/
function Bing_ Theme_activation () {
if ($GLOBALS [' Pagenow ']!= ' themes.php ' | |!isset ($_get[' activated ')) return;
/* Here you can place the code to be executed when the theme is enabled, such as jump to the settings interface:
wp_redirect (Admin_url (' options-general.php ')), and pay attention to modify the page address
die;
*
/} add_action (' load-themes.php ', ' bing_theme_activation ');
This code is valid in both the theme and the plugin.
Also note that updating a pseudo static rule is time-consuming and efficient, so don't call it every time you execute code, just call it if necessary (such as enabling themes and enabling plug-ins), and Hanging Flush_rewrite_rules () on the Init hook is extremely incorrect.
Usage
Flush_rewrite_rules ($hard);
Parameters
$hard
(Boolean) (optional) If true, the. htaccess file (hard flush) is refreshed together, and false only the pseudo static rules (soft flush) in the database are updated.
Default value: True (hard flush).
Example
To update a pseudo static rule when a theme is enabled:
function Bing_theme_activation () {
if ($GLOBALS [' Pagenow ']!= ' themes.php ' | |!isset ($_get[' activated ')) return;< C2/>flush_rewrite_rules ();
}
Add_action (' load-themes.php ', ' bing_theme_activation ');
To update a pseudo static rule when the plug-in is enabled:
function Bing_myplugin_activate () {
flush_rewrite_rules ();
}
Register_activation_hook (__file__, ' bing_myplugin_activate ');
Other
This function is located at: wp-includes/rewrite.php