: This article mainly introduces the PHP code instance used to update pseudo-static rules in WordPress. For more information about PHP tutorials, see. The flush_rewrite_rules () function is used to delete and rewrite Pseudo-static rules based on existing conditions, that is, to refresh a pseudo-static rule.
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
The above describes the PHP code examples used to update pseudo-static rules in WordPress, including some content, and hope to help those who are interested in PHP tutorials.