WP_Taxonomy::add_rewrite_rules() WordPress Method

The WP_Taxonomy::add_rewrite_rules() method allows you to add your own rewrite rules for a custom taxonomy. This is useful if you want to change the way your taxonomy URLs are structured. For example, you could use this method to add a rewrite rule that would allow you to access your taxonomy terms using a URL like example.com/taxonomy-term/term-slug.

WP_Taxonomy::add_rewrite_rules() #

Adds the necessary rewrite rules for the taxonomy.


Source

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

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


Top ↑

Changelog

Changelog
VersionDescription
4.7.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.