Example of PHP code used to update pseudo-static rules in WordPress _ php instance

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.