类文档

WP_REST_Term_Meta_Fields

💡 云策文档标注

概述

WP_REST_Term_Meta_Fields 是 WordPress 核心类,用于通过 REST API 管理分类法术语的元数据。它继承自 WP_REST_Meta_Fields,专门处理术语元数据操作。

关键要点

  • 继承自 WP_REST_Meta_Fields,专注于术语元数据管理
  • 构造函数接收 $taxonomy 参数指定分类法
  • 提供 get_meta_type() 返回 'term' 作为元类型
  • 提供 get_meta_subtype() 返回分类法名称作为元子类型
  • 提供 get_rest_field_type() 返回用于 register_rest_field() 的字段类型,特殊处理 'post_tag' 分类法

代码示例

class WP_REST_Term_Meta_Fields extends WP_REST_Meta_Fields {
    protected $taxonomy;

    public function __construct( $taxonomy ) {
        $this->taxonomy = $taxonomy;
    }

    protected function get_meta_type() {
        return 'term';
    }

    protected function get_meta_subtype() {
        return $this->taxonomy;
    }

    public function get_rest_field_type() {
        return 'post_tag' === $this->taxonomy ? 'tag' : $this->taxonomy;
    }
}

📄 原文内容

Core class used to manage meta values for terms via the REST API.

Description

See also

Methods

Name Description
WP_REST_Term_Meta_Fields::__construct Constructor.
WP_REST_Term_Meta_Fields::get_meta_subtype Retrieves the term meta subtype.
WP_REST_Term_Meta_Fields::get_meta_type Retrieves the term meta type.
WP_REST_Term_Meta_Fields::get_rest_field_type Retrieves the type for register_rest_field() .

Source

class WP_REST_Term_Meta_Fields extends WP_REST_Meta_Fields {

	/**
	 * Taxonomy to register fields for.
	 *
	 * @since 4.7.0
	 * @var string
	 */
	protected $taxonomy;

	/**
	 * Constructor.
	 *
	 * @since 4.7.0
	 *
	 * @param string $taxonomy Taxonomy to register fields for.
	 */
	public function __construct( $taxonomy ) {
		$this->taxonomy = $taxonomy;
	}

	/**
	 * Retrieves the term meta type.
	 *
	 * @since 4.7.0
	 *
	 * @return string The meta type.
	 */
	protected function get_meta_type() {
		return 'term';
	}

	/**
	 * Retrieves the term meta subtype.
	 *
	 * @since 4.9.8
	 *
	 * @return string Subtype for the meta type, or empty string if no specific subtype.
	 */
	protected function get_meta_subtype() {
		return $this->taxonomy;
	}

	/**
	 * Retrieves the type for register_rest_field().
	 *
	 * @since 4.7.0
	 *
	 * @return string The REST field type.
	 */
	public function get_rest_field_type() {
		return 'post_tag' === $this->taxonomy ? 'tag' : $this->taxonomy;
	}
}

Changelog

Version Description
4.7.0 Introduced.