函数文档

wp_font_dir()

💡 云策文档标注

概述

wp_font_dir() 函数返回一个数组,包含当前字体上传目录的路径和URL信息。它通过过滤 upload_dir 钩子来确保字体目录的一致性处理。

关键要点

  • 函数返回数组,包含 path、url、subdir、basedir、baseurl 和 error 等键值
  • 参数 $create_dir 可选,默认为 true,用于控制是否检查并创建字体上传目录
  • 内部使用 wp_upload_dir() 函数,并临时添加 _wp_filter_font_directory 过滤器到 upload_dir 钩子
  • 与 wp_get_font_dir() 相关,用于检索字体上传目录信息
  • 自 WordPress 6.5.0 版本引入

代码示例

function wp_font_dir( $create_dir = true ) {
    add_filter( 'upload_dir', '_wp_filter_font_directory' );
    $font_dir = wp_upload_dir( null, $create_dir, false );
    remove_filter( 'upload_dir', '_wp_filter_font_directory' );
    return $font_dir;
}

📄 原文内容

Returns an array containing the current fonts upload directory’s path and URL.

Parameters

$create_dirbooloptional
Whether to check and create the font uploads directory.

Default:true

Return

array Array of information about the font upload directory.

  • path string
    Base directory and subdirectory or full path to the fonts upload directory.
  • url string
    Base URL and subdirectory or absolute URL to the fonts upload directory.
  • subdir string
    Subdirectory
  • basedir string
    Path without subdir.
  • baseurl string
    URL path without subdir.
  • error string|false
    False or error message.

Source

function wp_font_dir( $create_dir = true ) {
	/*
	 * Allow extenders to manipulate the font directory consistently.
	 *
	 * Ensures the upload_dir filter is fired both when calling this function
	 * directly and when the upload directory is filtered in the Font Face
	 * REST API endpoint.
	 */
	add_filter( 'upload_dir', '_wp_filter_font_directory' );
	$font_dir = wp_upload_dir( null, $create_dir, false );
	remove_filter( 'upload_dir', '_wp_filter_font_directory' );
	return $font_dir;
}

Changelog

Version Description
6.5.0 Introduced.