函数文档

filter_default_metadata()

💡 云策文档标注

概述

filter_default_metadata() 是一个 WordPress 过滤器函数,用于在 default_{$object_type}_metadata 钩子中为元数据添加默认值。它根据元数据类型、键和对象 ID 检索并返回默认值,支持单值和数组返回。

关键要点

  • 函数作用:为元数据提供默认值,通过 default_{$object_type}_metadata 钩子调用。
  • 参数说明:接受 $value(当前值)、$object_id(对象 ID)、$meta_key(元数据键)、$single(是否返回单值)、$meta_type(元数据类型,如 'post'、'user' 等)。
  • 返回值:如果 $single 为 true,返回元字段的默认值;如果为 false,返回默认值数组。
  • 内部逻辑:检查 WordPress 是否在安装模式,验证全局 $wp_meta_keys 数组,根据元类型和子类型检索默认值。
  • 相关函数:与 get_object_subtype() 和 wp_installing() 关联,用于获取对象子类型和检查安装状态。

代码示例

function filter_default_metadata( $value, $object_id, $meta_key, $single, $meta_type ) {
    global $wp_meta_keys;

    if ( wp_installing() ) {
        return $value;
    }

    if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) {
        return $value;
    }

    $defaults = array();
    foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) {
        foreach ( $meta_data as $_meta_key => $args ) {
            if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) {
                $defaults[ $sub_type ] = $args;
            }
        }
    }

    if ( ! $defaults ) {
        return $value;
    }

    // If this meta type does not have subtypes, then the default is keyed as an empty string.
    if ( isset( $defaults[''] ) ) {
        $metadata = $defaults[''];
    } else {
        $sub_type = get_object_subtype( $meta_type, $object_id );
        if ( ! isset( $defaults[ $sub_type ] ) ) {
            return $value;
        }
        $metadata = $defaults[ $sub_type ];
    }

    if ( $single ) {
        $value = $metadata['default'];
    } else {
        $value = array( $metadata['default'] );
    }

    return $value;
}

注意事项

  • 函数在 WordPress 5.5.0 版本引入,需确保版本兼容性。
  • 依赖于全局变量 $wp_meta_keys,该数组需正确配置元数据默认值。
  • 在 WordPress 安装模式下,函数直接返回传入的 $value,不处理默认值。
  • 使用 get_object_subtype() 获取对象子类型,需确保元类型支持子类型。

📄 原文内容

Filters into default_{$object_type}_metadata and adds in default value.

Parameters

$valuemixedrequired
Current value passed to filter.
$object_idintrequired
ID of the object metadata is for.
$meta_keystringrequired
Metadata key.
$singleboolrequired
If true, return only the first value of the specified $meta_key.
This parameter has no effect if $meta_key is not specified.
$meta_typestringrequired
Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 'user', or any other object type with an associated meta table.

Return

mixed An array of default values if $single is false.
The default value of the meta field if $single is true.

Source

function filter_default_metadata( $value, $object_id, $meta_key, $single, $meta_type ) {
	global $wp_meta_keys;

	if ( wp_installing() ) {
		return $value;
	}

	if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) {
		return $value;
	}

	$defaults = array();
	foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) {
		foreach ( $meta_data as $_meta_key => $args ) {
			if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) {
				$defaults[ $sub_type ] = $args;
			}
		}
	}

	if ( ! $defaults ) {
		return $value;
	}

	// If this meta type does not have subtypes, then the default is keyed as an empty string.
	if ( isset( $defaults[''] ) ) {
		$metadata = $defaults[''];
	} else {
		$sub_type = get_object_subtype( $meta_type, $object_id );
		if ( ! isset( $defaults[ $sub_type ] ) ) {
			return $value;
		}
		$metadata = $defaults[ $sub_type ];
	}

	if ( $single ) {
		$value = $metadata['default'];
	} else {
		$value = array( $metadata['default'] );
	}

	return $value;
}

Changelog

Version Description
5.5.0 Introduced.