!
也想出现在这里? 联系我们
广告位
当前位置:首页>开发>WordPress开发手册>WordPress函数手册>WordPress函数文档flush_rewrite_rules()

WordPress函数文档flush_rewrite_rules()

更新伪静态规则 描述 Remove rewrite rules and then recreate rewri…--由S9社区整理

更新伪静态规则

描述

Remove rewrite rules and then recreate rewrite rules.

This function is useful when used with custom post types as it allows for automatic flushing of the WordPress rewrite rules (usually needs to be done manually for new custom post types). However, this is an expensive operation so it should only be used when absolutely necessary. See Usage section for more details.

用法

Important:

Flushing the rewrite rules is an expensive operation, there are tutorials and examples that suggest executing it on the ‘init’ hook. This is bad practice.

Flush rules only on activation or deactivation, or when you know that the rewrite rules need to be changed. Don’t do it on any hook that will triggered on a routine basis. More detail information in the comments on WP Engineer’s post: Custom Post Type and Permalink

Make sure custom post types and taxonomies are properly registered before flushing rewrite rules, especially during plugin activation: Activation Checklist for WordPress Plugin Developers

<?php flush_rewrite_rules( $hard ); ?>

参数

$hard

(boolean) (可选) Whether to update .htaccess (hard flush) or just update rewrite_rules transient (soft flush).

默认值: true (hard flush)

示例

This is how you would flush rewrite rules when a plugin is activated or deactivated:

1
2
3
4
5
6
7
8
9
10

/* ———————————-
* wordpress函数 kim收集
* ———————————- */

register_deactivation_hook( __FILE__, ‘flush_rewrite_rules’ );

register_activation_hook( __FILE__, ‘myplugin_flush_rewrites’ );

function myplugin_flush_rewrites() {

// call your CPT registration function here (it should also be hooked into ‘init’)

myplugin_custom_post_types_registration();

flush_rewrite_rules();

}

This is how you would flush rules on theme activation:

1
2
3
4
5

/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/* Flush rewrite rules for custom post types. */

add_action( ‘after_switch_theme’, ‘flush_rewrite_rules’ );

If you’re developing a theme, while building it you can use this snippet of code that will flush rewrite rules when the file containing it is changed, or every 48 hours:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/* ———————————-
* wordpress函数 kim收集
* ———————————- */
// do not use on live/production servers

add_action( ‘init’,‘maybe_rewrite_rules’ );

function maybe_rewrite_rules() {

$ver = filemtime( __FILE__ ); // Get the file time for this file as the version number

$defaults = array( ‘version’ => 0, ‘time’ => time() );

$r = wp_parse_args( get_option( __CLASS__ . ‘_flush’, array() ), $defaults );

if ( $r[‘version’] != $ver || $r[‘time’] + 172800 < time() ) { // Flush if ver changes or if 48hrs has passed.

flush_rewrite_rules();

// trace( ‘flushed’ );

$args = array( ‘version’ => $ver, ‘time’ => time() );

if ( ! update_option( __CLASS__ . ‘_flush’, $args ) )

add_option( __CLASS__ . ‘_flush’, $args );

}

}

历史

添加于 版本: 3.0

源文件

flush_rewrite_rules() 函数的代码位于 wp-includes/rewrite.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Remove rewrite rules and then recreate rewrite rules.
*
* @since 3.0.0
*
* @global WP_Rewrite $wp_rewrite
*
* @param bool $hard Whether to update .htaccess (hard flush) or just update
*                  rewrite_rules transient (soft flush). Default is true (hard).
*/

function flush_rewrite_rules( $hard = true ) {

global $wp_rewrite;

$wp_rewrite->flush_rules( $hard );

}

原文:http://codex.wordpress.org/Function_Reference/flush_rewrite_rules

给TA打赏
共{{data.count}}人
人已打赏
WordPress函数手册

WordPress函数文档get_active_blog_for_user()

2023-6-16 19:38:49

WordPress函数手册

WordPress函数文档fix_phpmailer_messageid()

2023-6-16 19:39:21

声明 本站上的部份代码及教程来源于互联网,仅供网友学习交流,若您喜欢本文可附上原文链接随意转载。无意侵害您的权益,请发送邮件至 [email protected] 或点击右侧 私信:林沐阳 反馈,我们将尽快处理。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索