Examples of PHP code for updating pseudo static rules in WordPress explain _php

Source: Internet
Author: User
Tags php code

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

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.