最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Wordpress 5.4 - How to prevent to enter only certain values in custom field

programmeradmin5浏览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.

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?

Share Improve this question asked Jul 21, 2020 at 4:21 VishalVishal 1012 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/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.

发布评论

评论列表(0)

  1. 暂无评论