WordPress version - 5.4.2
I am creating one WordPress plugin and in this plugin, I need one metadata for the post. I added meta box accesstype visibility
successfully. It is a select box and it has only fixed 3 values. it is working fine in block editor as shown below.
However, when I enable custom fields by going in options. it is allowing me to add any value in the text box and it will save it as shown below.
How can I hide accesstype visibility
from it or add a select box instead of a text box?
WordPress version - 5.4.2
I am creating one WordPress plugin and in this plugin, I need one metadata for the post. I added meta box accesstype visibility
successfully. It is a select box and it has only fixed 3 values. it is working fine in block editor as shown below.
However, when I enable custom fields by going in options. it is allowing me to add any value in the text box and it will save it as shown below.
How can I hide accesstype visibility
from it or add a select box instead of a text box?
1 Answer
Reset to default 1You can make the meta data private, and not visible on the Custom Fields list, by appending underscore to the meta key. I.e. _accesstype_visibility
You can find more details in the WP Developer docs, https://developer.wordpress/plugins/metadata/managing-post-metadata/#hidden-custom-fields
EDIT 24.7.2020
Here's an example how to use is_protected_meta()
filter to make meta key public or private. You can read more about filters here, https://developer.wordpress/plugins/hooks/filters/
// first hook we're filtering, second our callback, third priority, fourth # of parameters
add_filter( 'is_protected_meta', 'my_prefix_is_protected_meta', 10, 3 );
// available parameters can be found on the filter documentation
function my_prefix_is_protected_meta( $protected, $meta_key, $meta_type ) {
// Force custom meta key to be protected
if ( 'my_meta_key' === $meta_key ) {
$protected = true;
}
// Return filtered value so WP can continue whatever it was doing with the value
return $protected;
}
You can use this in your theme's functions.php
file or in a custom plugin.