Using WordPress ‘block_editor_meta_box_hidden_fields’ PHP action

The block_editor_meta_box_hidden_fields WordPress action allows you to add hidden input fields to the meta box save form.

Usage

add_action('block_editor_meta_box_hidden_fields', 'my_custom_hidden_fields', 10, 1);

function my_custom_hidden_fields($post) {
    // your custom code here

    return $post;
}

Parameters

  • $post (WP_Post) – The post that is being edited.

More information

See WordPress Developer Resources: block_editor_meta_box_hidden_fields

Examples

Add a nonce field

Add a nonce field to protect against CSRF attacks.

add_action('block_editor_meta_box_hidden_fields', 'add_nonce_field_to_meta_box', 10, 1);

function add_nonce_field_to_meta_box($post) {
    wp_nonce_field('my_custom_nonce_action', 'my_custom_nonce_name');
}

Add a hidden post ID field

Add a hidden field with the post ID for later reference.

add_action('block_editor_meta_box_hidden_fields', 'add_post_id_hidden_field', 10, 1);

function add_post_id_hidden_field($post) {
    echo '<input type="hidden" name="my_custom_post_id" value="' . $post->ID . '">';
}

Add a hidden field with post status

Add a hidden field with the current post status.

add_action('block_editor_meta_box_hidden_fields', 'add_post_status_hidden_field', 10, 1);

function add_post_status_hidden_field($post) {
    echo '<input type="hidden" name="my_custom_post_status" value="' . $post->post_status . '">';
}

Add multiple hidden fields

Add multiple hidden fields at once.

add_action('block_editor_meta_box_hidden_fields', 'add_multiple_hidden_fields', 10, 1);

function add_multiple_hidden_fields($post) {
    echo '<input type="hidden" name="hidden_field_1" value="value_1">';
    echo '<input type="hidden" name="hidden_field_2" value="value_2">';
}

Add a hidden field with custom data

Add a hidden field with custom data based on the post.

add_action('block_editor_meta_box_hidden_fields', 'add_custom_data_hidden_field', 10, 1);

function add_custom_data_hidden_field($post) {
    $custom_data = get_post_meta($post->ID, 'my_custom_data', true);
    echo '<input type="hidden" name="my_custom_data_field" value="' . esc_attr($custom_data) . '">';
}