0

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.

enter image description here

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.

enter image description here

How can I hide accesstype visibility from it or add a select box instead of a text box?

1

You 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.org/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.org/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.

5
  • I tried your solution, it is not visible in custom fields any more though it is still visible in the dropdown of add new custom fields. It's not a complete solution. anyways, i tried to hide it by JS and its working fine for now. don't know whether its best solution or not but it worked for me
    – Vishal
    Jul 22 '20 at 5:36
  • @Vishal is it the accesstype_visibility or _accesstype_visibility that is visible in the custom fields meta select? You should be able to brute force the key out of the list with is_protected_meta filter, developer.wordpress.org/reference/functions/is_protected_meta Jul 22 '20 at 11:13
  • It is visible in both cases. I am very new to WordPress not sure how and where to use this is_protected_meta filter, and what exactly it will do.
    – Vishal
    Jul 24 '20 at 6:02
  • @Vishal What happens, if you remove the meta data which has the public key? I.e. execute delete_post_meta($post_id, 'accesstype_visibility'); once before saving meta data with the private key? Jul 24 '20 at 10:28
  • I added a example on how to use the filter to my answer, if you're interested. Jul 24 '20 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.