WP_Rewrite::wp_rewrite_rules() WordPress Method

The WP_Rewrite::wp_rewrite_rules() method is used to generate the rewrite rules for the WordPress permalink structure. This is a powerful method that allows you to customize the way your permalinks are structured. You can use this method to create custom rewrite rules for your WordPress site.

WP_Rewrite::wp_rewrite_rules() #

Retrieves the rewrite rules.


Description

The difference between this method and WP_Rewrite::rewrite_rules() is that this method stores the rewrite rules in the ‘rewrite_rules’ option and retrieves it. This prevents having to process all of the permalinks to get the rewrite rules in the form of caching.


Top ↑

Return

(string[]) Array of rewrite rules keyed by their regex pattern.


Top ↑

More Information

It returns the array of rewrite rules as in rewrite_rules(), but using $matches[xxx] in the (where xxx is a number) instead of the normal mod_rewrite backreferences, $xxx (where xxx is a number). This is useful when you’re going to be using the rules inside PHP, rather than writing them out to a .htaccess file.


Top ↑

Source

File: wp-includes/class-wp-rewrite.php

	public function wp_rewrite_rules() {
		$this->rules = get_option( 'rewrite_rules' );
		if ( empty( $this->rules ) ) {
			$this->matches = 'matches';
			$this->rewrite_rules();
			if ( ! did_action( 'wp_loaded' ) ) {
				add_action( 'wp_loaded', array( $this, 'flush_rules' ) );
				return $this->rules;
			}
			update_option( 'rewrite_rules', $this->rules );
		}

		return $this->rules;
	}


Top ↑

Changelog

Changelog
VersionDescription
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.