WP_Post_Type::add_rewrite_rules()publicWP 4.6.0

Adds the necessary rewrite rules for the post type.

Метод класса: WP_Post_Type{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

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

Заметки

  • Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.
  • Global. WP. $wp Current WordPress environment instance.

Список изменений

С версии 4.6.0 Введена.

Код WP_Post_Type::add_rewrite_rules() WP 6.6.2

public function add_rewrite_rules() {
	global $wp_rewrite, $wp;

	if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
		$wp->add_query_var( $this->query_var );
	}

	if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
		if ( $this->hierarchical ) {
			add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" );
		} else {
			add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
		}

		if ( $this->has_archive ) {
			$archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive;
			if ( $this->rewrite['with_front'] ) {
				$archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
			} else {
				$archive_slug = $wp_rewrite->root . $archive_slug;
			}

			add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' );
			if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) {
				$feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
				add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
				add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
			}
			if ( $this->rewrite['pages'] ) {
				add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' );
			}
		}

		$permastruct_args         = $this->rewrite;
		$permastruct_args['feed'] = $permastruct_args['feeds'];
		add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args );
	}
}