flush_rewrite_rules()

最后更新于:2021-11-26 04:08:03

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 option (soft flush). Default is true (hard).

Default value: true

源文件

文件: gc-includes/rewrite.php

function flush_rewrite_rules( $hard = true ) {
	global $gc_rewrite;

	if ( is_callable( array( $gc_rewrite, 'flush_rules' ) ) ) {
		$gc_rewrite->flush_rules( $hard );
	}
}
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 $gc;

	$gc->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);

	}
}
function gcdoc_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', 'gcdoc_flush_rules_on_save_posts', 20, 2);
// 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 = gc_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 );
	}

}

register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
register_activation_hook( __FILE__, 'wdocs_flush_rewrites' );


/**
 * Flush rewrite rules on activation
 */
function gcdocs_flush_rewrites() {
	// call your CPT registration function here (it should also be hooked into 'init')
	gcdocs_custom_post_types_registration();
	flush_rewrite_rules();
}


// flush rules and serve new rules instantly without page refresh
add_action('init', function() {
    flush_rewrite_rules();
});
// Or, hook into gc tag, flush rules and do an internal refresh
add_action('gc', 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.
        gc_redirect( $_SERVER['REQUEST_URI'] );
        exit;
    }
});