函数文档

wp_register_font_collection()

💡 云策文档标注

概述

wp_register_font_collection() 函数用于在 WordPress 字体库中注册一个新的字体集合。它接受字体集合的 slug 和数据参数,并返回一个 WP_Font_Collection 对象或 WP_Error 对象。

关键要点

  • 函数签名:wp_register_font_collection( string $slug, array $args )
  • 参数 $slug:字体集合的 slug,只能包含字母数字、短横线和下划线,需符合 sanitize_title() 规范。
  • 参数 $args:字体集合数据数组,包括必填的 name 和 font_families,以及可选的 description 和 categories。
  • 返回值:成功时返回 WP_Font_Collection 对象,失败时返回 WP_Error 对象。
  • 字体集合数据必须遵循特定 JSON 模式,参考 https://schemas.wp.org/trunk/font-collection.json。
  • font_families 可以是字体家族定义的数组,或指向包含字体集合的 JSON 文件的路径/URL 字符串。

代码示例

function wp_register_font_collection( string $slug, array $args ) {
    return WP_Font_Library::get_instance()->register_font_collection( $slug, $args );
}

注意事项

  • 此函数在 WordPress 6.5.0 版本中引入。
  • 相关函数包括 _wp_register_default_font_collections() 用于注册默认字体集合。

📄 原文内容

Registers a new font collection in the font library.

Description

See https://schemas.wp.org/trunk/font-collection.json for the schema the font collection data must adhere to.

Parameters

$slugstringrequired
Font collection slug. May only contain alphanumeric characters, dashes, and underscores. See sanitize_title() .
$argsarrayrequired
Font collection data.

  • name string
    Required. Name of the font collection shown in the Font Library.
  • description string
    Optional. A short descriptive summary of the font collection. Default empty.
  • font_families array|string
    Required. Array of font family definitions that are in the collection, or a string containing the path or URL to a JSON file containing the font collection.
  • categories array
    Optional. Array of categories, each with a name and slug, that are used by the fonts in the collection. Default empty.

Return

WP_Font_Collection|WP_Error A font collection if it was registered successfully, or WP_Error object on failure.

Source

function wp_register_font_collection( string $slug, array $args ) {
	return WP_Font_Library::get_instance()->register_font_collection( $slug, $args );
}

Changelog

Version Description
6.5.0 Introduced.