This article mainly introduces the PHP code example for updating pseudo-static rules in WordPress. For details about how to use the flush_rewrite_rules () function, refer to flush_rewrite_rules () the function is used to delete and rewrite pseudo-static rules based on the existing conditions, that is, refresh the pseudo-static rules.
First, it is usually called when the topic or plug-in adds a new custom Article type to prevent 404 of the new custom Article type articles, or, in many cases, we need to execute some code when the topic is enabled, such as assigning some database forms and redirecting to the settings page. WordPress does not provide related hooks, there are also a variety of implementation methods on the Internet. After my research, I found that it may be the best method. I will share it with you below:
/*** Execute some code when the WordPress topic is enabled * http://www.endskin.com/theme-activation-action/#/function Bing_theme_activation () {if ($ GLOBALS ['pagenow']! = 'Themes. php' |! Isset ($ _ GET ['active']) return;/* the code to be executed when the topic is enabled. For example, you can jump to the setting page: wp_redirect (admin_url ('Options-general. php '); // modify the page address die; */} add_action ('Load-themes. php ', 'bing _ theme_activation ');
This code is effective in themes and plug-ins.
In addition, it takes a lot of time and efficiency to update pseudo-static rules, so do not call the code every time you execute it, only when necessary (for example, enable the topic and plug-in ), mounting flush_rewrite_rules () to the init hook is extremely incorrect.
Usage
flush_rewrite_rules( $hard );
Parameters
$ Hard
(Boolean) (optional) if it is True, refresh the. htaccess file (hard flush) together. If it is False, only the pseudo static rules (soft flush) in the database are updated ).
Default Value: True (hard flush ).
Example
Update pseudo-static rules when the topic is Enabled:
function Bing_theme_activation(){ if( $GLOBALS['pagenow'] != 'themes.php' || !isset( $_GET['activated'] ) ) return; flush_rewrite_rules();}add_action( 'load-themes.php', 'Bing_theme_activation' );
Update pseudo-static rules when the plug-in is Enabled:
function Bing_myplugin_activate(){ flush_rewrite_rules();}register_activation_hook( __FILE__, 'Bing_myplugin_activate' );
Others
This function is located in: wp-nodes des/rewrite. php