flush_rewrite_rules() - WordPress 函数参考中文文档

返回介绍

flush_rewrite_rules()

发布于 2017-09-10 字数4606 浏览 911 评论 0

flush_rewrite_rules( bool $hard = true )

Remove rewrite rules and then recreate rewrite rules.


描述


参数

$hard

(bool)
(Optional)
Whether to update .htaccess (hard flush) or just update rewrite_rules transient (soft flush). Default is true (hard).

Default value: true


源代码

File: wp-includes/rewrite.php

function flush_rewrite_rules( $hard = true ) {
	global $wp_rewrite;
	$wp_rewrite->flush_rules( $hard );
}

更新日志

Version 描述
3.0.0 Introduced.

相关函数

Uses

  • wp-includes/class-wp-rewrite.php:
    WP_Rewrite::flush_rules()

Used By

  • wp-admin/includes/misc.php:
    update_home_siteurl()
  • wp-admin/includes/schema.php:
    populate_network()
  • wp-admin/includes/upgrade.php:
    wp_install()
  • wp-includes/theme.php:
    check_theme_switched()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

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

    
    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();
    }
    
    
    
  2. This is how you would flush rules on theme activation:

    
    /* 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:

    
    // 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() {
    
    	$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 );
    	}
    
    }
    
    

    If you want to flush rules while updating posts based on post type:

    
    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);
    
    
    // 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;
        }
    });
    

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!