Using WordPress ‘is_protected_meta’ PHP filter

The is_protected_meta WordPress PHP Filter determines if a meta key is considered protected.

Usage

add_filter('is_protected_meta', 'my_custom_function', 10, 3);
function my_custom_function($protected, $meta_key, $meta_type) {
    // your custom code here
    return $protected;
}

Parameters

  • $protected (bool): Whether the key is considered protected.
  • $meta_key (string): Metadata key.
  • $meta_type (string): Type of object metadata is for. Accepts ‘post’, ‘comment’, ‘term’, ‘user’, or any other object type with an associated meta table.

More information

See WordPress Developer Resources: is_protected_meta

Examples

Protect a Custom Meta Key for Posts

Protect the custom meta key ‘my_secret_key’ for posts.

add_filter('is_protected_meta', 'protect_my_secret_key', 10, 3);
function protect_my_secret_key($protected, $meta_key, $meta_type) {
    if ('post' === $meta_type && 'my_secret_key' === $meta_key) {
        return true;
    }
    return $protected;
}

Protect a Custom Meta Key for Users

Protect the custom meta key ‘user_secret_key’ for users.

add_filter('is_protected_meta', 'protect_user_secret_key', 10, 3);
function protect_user_secret_key($protected, $meta_key, $meta_type) {
    if ('user' === $meta_type && 'user_secret_key' === $meta_key) {
        return true;
    }
    return $protected;
}

Unprotect Default Post Meta Key

Unprotect the default meta key ‘_edit_lock’ for posts.

add_filter('is_protected_meta', 'unprotect_edit_lock', 10, 3);
function unprotect_edit_lock($protected, $meta_key, $meta_type) {
    if ('post' === $meta_type && '_edit_lock' === $meta_key) {
        return false;
    }
    return $protected;
}

Unprotect All Meta Keys for Comments

Unprotect all meta keys for comments.

add_filter('is_protected_meta', 'unprotect_comment_meta_keys', 10, 3);
function unprotect_comment_meta_keys($protected, $meta_key, $meta_type) {
    if ('comment' === $meta_type) {
        return false;
    }
    return $protected;
}

Protect Meta Keys Based on a Prefix

Protect all meta keys with the prefix ‘protected_’.

add_filter('is_protected_meta', 'protect_meta_keys_with_prefix', 10, 3);
function protect_meta_keys_with_prefix($protected, $meta_key, $meta_type) {
    if (0 === strpos($meta_key, 'protected_')) {
        return true;
    }
    return $protected;
}