Wordpress Metadata Values ​​Displayed in Custom Fields

I added meta fields to my posts using the add_meta_boxes action to add / change user preferences like background-color etc.

When I include custom fields in my screen settings, all the values ​​of my meta fields are displayed in these custom fields!

An icon also appears in the selection item to add a new "custom field."

+2
source share
2 answers

If you want to hide your message metadata from custom metabox fields, you must start your meta keys with underscores. _background-color example

Added:

You can also use the is_protected_meta filter, which returns boolean ( true - hide, false - show ).

Filter options: $protected, $meta_key . See wp-includes/meta.php . function is_protected_meta()

+3
source

Typically, WP hides meta keys that begin with an underscore / _ from the custom field (default / core) of the MetaBox.

Now imagine that you do not want the user of your plugin to be able to modify Meta data due to the incorrect and unfriendly metadata of Custom Fields. And so you create a custom meta field and a prefix for your meta key with an underscore / _ . Then the user changes his mind and deactivates or removes your plugin. What is happening now is that the user has absolutely no access to any user interface in order to change the (still present) metadata. This is really a very bad situation for the user.

So, we need a switch to disable access to MetaBox Custom Fields while your plugin is activated . Therefore, WP Core got the function is_protected_meta() . It consists mainly of two lines of code:

 $protected = ( '_' == $meta_key[0] ); return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type ); 

It would be nice to offer a filter for processing, WordPress today has a simple function that you can use:

 register_meta( $meta_type, $key, $sanitize_callback, $auth_callback ); 

And the last argument, $auth_callback does the following inside this function:

 if ( empty( $auth_callback ) ) { if ( is_protected_meta( $meta_key, $meta_type ) ) $auth_callback = '__return_false'; else $auth_callback = '__return_true'; } if ( is_callable( $auth_callback ) ) add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 ); 

As you can see, you just want to add '__return_false' as $auth_callback to deactivate access to custom MetaBox fields while your plugin is active. When a user deletes or deactivates your plugin, he instantly gets access to the meta-field through the standard custom text MetaBox.


Notes: WP core in version 4.0 when writing this question. Use $sanitize_callback ! Thanks to Trepmal for posting the is_protected_meta filter on my blog. Otherwise, I would never have come across a precedent for this.

+1
source

Source: https://habr.com/ru/post/1487155/


All Articles