钩子文档

sanitize_{$meta_type}_meta_{$meta_key}

💡 云策文档标注

概述

sanitize_{$meta_type}_meta_{$meta_key} 是一个动态 Hook,用于过滤特定元类型(如评论、文章或用户)中特定元键的元值清理过程。它允许开发者在元数据保存前自定义清理逻辑。

关键要点

  • 这是一个动态 Hook,名称中的 $meta_type 和 $meta_key 分别指代元类型(如 comment、post、user)和元键值。
  • Hook 参数包括 $meta_value(要清理的元值)、$meta_key(元键)和 $meta_type(元类型)。
  • 在 WordPress 3.3.0 版本中引入,用于增强元数据处理的灵活性。

代码示例

add_filter( 'sanitize_post_meta_my_custom_key', 'my_custom_sanitize_function', 10, 3 );
function my_custom_sanitize_function( $meta_value, $meta_key, $meta_type ) {
    // 自定义清理逻辑,例如去除 HTML 标签
    return wp_strip_all_tags( $meta_value );
}

注意事项

使用此 Hook 时,需确保动态部分 $meta_type 和 $meta_key 与实际元类型和元键匹配,以避免 Hook 不生效。清理后的元值应返回适当格式,如字符串或序列化数据。


📄 原文内容

Filter the sanitization of a specific meta key of a specific meta type.

Description

The dynamic portions of the hook name, $meta_type, and $meta_key, refer to the metadata object type (comment, post, or user) and the meta key value, respectively.

Parameters

$meta_valuemixed
Meta value to sanitize.
$meta_keystring
Meta key.
$meta_typestring
Meta type.

Source

$meta_value  = maybe_serialize( $meta_value );

Changelog

Version Description
3.3.0 Introduced.