Register Meta Field Edit

A post meta field is a WordPress object used to store extra data about a post. You need to first register a new meta field prior to use. See Managing Post Metadata to learn more about post meta.

When registering the field, note the show_in_rest parameter. This ensures the data will be included in the REST API, which the block editor uses to load and save meta data. See the register_post_meta function definition for extra information.

Additionally, your post type needs to support custom-fields for register_post_meta function to work

To register the field, create a PHP plugin file called myguten-meta-block.php including:

<?php
/**
 * Plugin Name: Meta Block
 */

// register custom meta tag field
function myguten_register_post_meta() {
    register_post_meta( 'post', 'myguten_meta_block_field', array(
        'show_in_rest' => true,
        'single' => true,
        'type' => 'string',
    ) );
}
add_action( 'init', 'myguten_register_post_meta' );

Note: If the meta key name starts with an underscore WordPress considers it a protected field. Editing this field requires passing a permission check, which is set as the auth_callback in the register_post_meta function. Here is an example:

register_post_meta( 'post', '_myguten_protected_key', array(
    'show_in_rest' => true,
    'single' => true,
    'type' => 'string',
    'auth_callback' => function() {
        return current_user_can( 'edit_posts' );
    }
) );