Using WordPress ‘is_protected_meta()’ PHP function

The is_protected_meta() WordPress PHP function determines whether a meta key is considered protected.

Usage

is_protected_meta( $meta_key, $meta_type = '' );

Parameters

  • $meta_key (string) – Required. The metadata key.
  • $meta_type (string) – Optional. Type of object metadata is for. Accepts ‘post’, ‘comment’, ‘term’, ‘user’, or any other object type with an associated meta table. Default: ”

More information

See WordPress Developer Resources: is_protected_meta()

Examples

Check if a post meta key is protected

// Check if 'my_protected_key' is a protected meta key for posts
$meta_key = 'my_protected_key';
$is_protected = is_protected_meta( $meta_key, 'post' );

// Output the result
if ( $is_protected ) {
    echo '**' . $meta_key . '** is a protected meta key.';
} else {
    echo '**' . $meta_key . '** is not a protected meta key.';
}

Check if a user meta key is protected

// Check if 'my_user_key' is a protected meta key for users
$meta_key = 'my_user_key';
$is_protected = is_protected_meta( $meta_key, 'user' );

// Output the result
if ( $is_protected ) {
    echo '**' . $meta_key . '** is a protected meta key.';
} else {
    echo '**' . $meta_key . '** is not a protected meta key.';
}

Check if a term meta key is protected

// Check if 'my_term_key' is a protected meta key for terms
$meta_key = 'my_term_key';
$is_protected = is_protected_meta( $meta_key, 'term' );

// Output the result
if ( $is_protected ) {
    echo '**' . $meta_key . '** is a protected meta key.';
} else {
    echo '**' . $meta_key . '** is not a protected meta key.';
}

Check if a comment meta key is protected

// Check if 'my_comment_key' is a protected meta key for comments
$meta_key = 'my_comment_key';
$is_protected = is_protected_meta( $meta_key, 'comment' );

// Output the result
if ( $is_protected ) {
    echo '**' . $meta_key . '** is a protected meta key.';
} else {
    echo '**' . $meta_key . '** is not a protected meta key.';
}

Check if a custom object type meta key is protected

// Check if 'my_custom_key' is a protected meta key for a custom object type called 'my_custom_object_type'
$meta_key = 'my_custom_key';
$meta_type = 'my_custom_object_type';
$is_protected = is_protected_meta( $meta_key, $meta_type );

// Output the result
if ( $is_protected ) {
    echo '**' . $meta_key . '** is a protected meta key for **' . $meta_type . '**.';
} else {
    echo '**' . $meta_key . '** is not a protected meta key for **' . $meta_type . '**.';
}