WP_Taxonomy::add_rewrite_rules()publicWP 4.7.0

Adds the necessary rewrite rules for the taxonomy.

Method of the class: WP_Taxonomy{}

No Hooks.

Return

null. Nothing (null).

Usage

$WP_Taxonomy = new WP_Taxonomy();
$WP_Taxonomy->add_rewrite_rules();

Notes

  • Global. WP. $wp Current WordPress environment instance.

Changelog

Since 4.7.0 Introduced.

WP_Taxonomy::add_rewrite_rules() code WP 6.5.2

public function add_rewrite_rules() {
	/* @var WP $wp */
	global $wp;

	// Non-publicly queryable taxonomies should not register query vars, except in the admin.
	if ( false !== $this->query_var && $wp ) {
		$wp->add_query_var( $this->query_var );
	}

	if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
		if ( $this->hierarchical && $this->rewrite['hierarchical'] ) {
			$tag = '(.+?)';
		} else {
			$tag = '([^/]+)';
		}

		add_rewrite_tag( "%$this->name%", $tag, $this->query_var ? "{$this->query_var}=" : "taxonomy=$this->name&term=" );
		add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $this->rewrite );
	}
}