当前浏览:首页 / WordPress函数 / flush_rewrite_rules()

flush_rewrite_rules( bool $hard = true )

删除重写规则,然后重新创建重写规则(即刷新重写规则)

rewrite


参数

$hard

(bool)(可选) 是更新.htaccess(硬刷新)还是只更新rewrite_rules选项(软刷新)。默认值为true(硬刷新)。

默认值: true


更多信息

当与自定义文章类型一起使用时,此函数非常有用,因为它允许自动刷新WordPress重写规则(通常需要手动完成新的自定义文章类型)。但是,这是一个昂贵的操作,因此只能在必要时使用。



源码

查看源码 官方文档


更新日志

版本描述
3.0.0开始引入

使用示例

  • 示例1
    register_activation_hook( __FILE__, 'plugin_activation' );
    function plugin_activation() {
    	update_option('plugin_permalinks_flushed', 0);
    }
    
    add_action( 'init', 'custom_query_vars' );
    function custom_query_vars() {
    	global $wp;
    
    	$wp->add_query_var( 'newfeed' );
    	add_rewrite_rule( '^newfeed/([^/]*)/?', 'index.php?newfeed=$matches[1]', 'top' );
    
    	if( !get_option('plugin_permalinks_flushed') ) {
    
    		flush_rewrite_rules(false);
    		update_option('plugin_permalinks_flushed', 1);
    
    	}
    }
    
  • 示例2

    如果要在基于文章类型更新文章时刷新规则:

    function wpdoc_flush_rules_on_save_posts( $post_id ) {
    
        // Check the correct post type.
        // Example to check, if the post type isn't 'post' then don't flush, just return.
        if ( ! empty( $_POST['post_type'] && $_POST['post_type'] != 'post' ) {
            return;
        }
    
        flush_rewrite_rules();
    
    }
    
    add_action( 'save_post', 'wpdoc_flush_rules_on_save_posts', 20, 2);
    
  • 示例3

    这是在主题激活时如何刷新规则:

    /* Flush rewrite rules for custom post types. */
    add_action( 'after_switch_theme', 'flush_rewrite_rules' );
    
    
  • 示例4

    如果您正在开发一个主题,在构建它时,您可以使用以下代码片段,当包含该主题的文件发生更改时,或每48小时刷新一次重写规则:

    // do not use on live/production servers
    add_action( 'init','maybe_rewrite_rules' );
    
    /**
     * Flush rewrite rules if the current file has changed and at least every 48 hours.
     */
    function maybe_rewrite_rules() {
    	if ( ! is_admin() ) {
    		return;
    	}
    
    	$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 );
    	}
    
    }
    
    
  • 示例5

    在激活和停用挂钩上的刷新重写规则的一种相对简单的方法是根本不使用flush_rewrite_rules()。相反,只需清除rewrite_rules选项即可强制WordPress在正确的时间重新创建它们。

    例子:

    register_activation_hook( __FILE__, 'wpdocs_wpdev_activate' );
    
    function wpdocs_wpdev_activate() {
    	// force rewrite rules to be recreated at the right time
    	delete_option( 'rewrite_rules' );
    }
    

    这就避免了像其他评论中建议的那样,在激活钩子中注册自定义文章类型,或抛出并捕获专用db选项或瞬态等复杂操作…

  • 示例6

    这是在激活或停用插件时刷新重写规则的方式:

    register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
    register_activation_hook( __FILE__, 'wdocs_flush_rewrites' );
    
    
    /**
     * Flush rewrite rules on activation
     */
    function wpdocs_flush_rewrites() {
    	// call your CPT registration function here (it should also be hooked into 'init')
    	wpdocs_custom_post_types_registration();
    	flush_rewrite_rules();
    }
    
    
    
  • 示例7
    // flush rules and serve new rules instantly without page refresh
    add_action('init', function() {
        flush_rewrite_rules();
    });
    // Or, hook into wp tag, flush rules and do an internal refresh
    add_action('wp', function() {
        if( "1" !== get_option("my_rules_have_been_flushed") ) {
            flush_rewrite_rules();
            update_option('my_rules_have_been_flushed', '1');
            // now, redirect the request.
            wp_redirect( $_SERVER['REQUEST_URI'] );
            exit;
        }
    });