is_protected_meta()
Determines whether a meta key is protected (internal).
WordPress uses the following mechanism: if the name of the meta key starts with an underscore _, for example, _edit_time, then it's considered as protected - it means this meta key is used for internal purposes, so the protected meta keys are not displayed in the Custom Fields panel when editing a post.
1 time — 0.00001 sec (speed of light) | 50000 times — 0.03 sec (speed of light) | PHP 7.0.8, WP 4.6
Hooks from the function
Returns
true|false. Whether the meta key is considered protected.
Usage
is_protected_meta( $meta_key, $meta_type );
- $meta_key(string) (required)
- 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.
Default: empty string
Examples
#1 Check whether it is a protected meta key
$meta_key = '_my_metakey';
if( is_protected_meta( $meta_key, 'post' ) ){
echo 'Protected meta key';
}
Changelog
| Since 3.1.3 | Introduced. |
is_protected_meta() is protected meta code WP 6.8.3
function is_protected_meta( $meta_key, $meta_type = '' ) {
$sanitized_key = preg_replace( "/[^\x20-\x7E\p{L}]/", '', $meta_key );
$protected = strlen( $sanitized_key ) > 0 && ( '_' === $sanitized_key[0] );
/**
* Filters whether a meta key is considered protected.
*
* @since 3.2.0
*
* @param bool $protected Whether the key is considered protected.
* @param string $meta_key Metadata key.
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
*/
return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
}