钩子文档

is_protected_meta

💡 云策文档标注

概述

is_protected_meta 是一个 WordPress 过滤器,用于控制是否将某个元数据键视为受保护的。开发者可以通过此过滤器自定义特定元键的保护状态,防止其在某些界面(如自定义字段)中显示。

关键要点

  • 过滤器名称:is_protected_meta
  • 参数:$protected(布尔值,表示当前保护状态)、$meta_key(字符串,元数据键)、$meta_type(字符串,元数据类型,如 'post'、'user' 等)
  • 返回:应用过滤器后的 $protected 值
  • 用途:常用于隐藏敏感或内部元数据,避免用户意外修改

代码示例

function my_meta_is_protected_meta( $protected, $meta_key, $meta_type ) {
        switch ($meta_key) {
                case 'my_first_meta':
                        $protected = true;
                        break;
                case 'my_second_meta':
                        $protected = true;
                        break;
        }
        return $protected;
}
add_filter( 'is_protected_meta', 'my_meta_is_protected_meta', 10, 3 );

📄 原文内容

Filters whether a meta key is considered protected.

Parameters

$protectedbool
Whether the key is considered protected.
$meta_keystring
Metadata key.
$meta_typestring
Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 'user', or any other object type with an associated meta table.

Source

return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );

Changelog

Version Description
3.2.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example of 2 meta fields that you want to set as protected:

    function my_meta_is_protected_meta( $protected, $meta_key, $meta_type ) {
    
            switch ($meta_key) {
                    case 'my_first_meta':
                            $protected = true;
                            break;
                    case 'my_second_meta':
                            $protected = true;
                            break;
            }
    
            return $protected;
    }
    add_filter( 'is_protected_meta', 'my_meta_is_protected_meta', 10, 3 );