Add a custom meta box to the WordPress post editor with secure saving
A meta box is a panel in the post editor for your own data attached to a post, page or custom post type. It is the classic way to add custom fields, and it still works in the block editor: add_meta_box() boxes are shown under the content or in the sidebar. The box outputs a form, and the data is saved on save_post. The important part is the saving: without the security checks, the handler can be abused to change posts.
Problem: Add your own fields (a subtitle and a reading time) to the post editor and save them safely.
Solution: Register the box with add_meta_box(), output a nonce field in it, and in the save_post handler check the nonce, autosave, revisions and the user’s rights before saving the sanitized values with update_post_meta().
The code
In functions.php or a plugin:
<?php
// 1. Register the box
add_action( 'add_meta_boxes', 'helloadmin_register_meta_box' );
function helloadmin_register_meta_box() {
add_meta_box(
'helloadmin_post_details', // ID
__( 'Post Details', 'helloadmin' ), // title
'helloadmin_meta_box_callback', // output function
array( 'post', 'page' ), // post types
'side', // context: normal | side | advanced
'high' // priority: high | default | low
);
}
// 2. Output the fields
function helloadmin_meta_box_callback( $post ) {
wp_nonce_field( 'helloadmin_save_meta', 'helloadmin_meta_nonce' );
$subtitle = get_post_meta( $post->ID, '_helloadmin_subtitle', true );
$read_min = get_post_meta( $post->ID, '_helloadmin_read_min', true );
?>
<p>
<label for="helloadmin_subtitle"><strong><?php esc_html_e( 'Subtitle', 'helloadmin' ); ?></strong></label><br>
<input type="text" id="helloadmin_subtitle" name="helloadmin_subtitle" value="<?php echo esc_attr( $subtitle ); ?>" style="width:100%">
</p>
<p>
<label for="helloadmin_read_min"><strong><?php esc_html_e( 'Read time (minutes)', 'helloadmin' ); ?></strong></label><br>
<input type="number" id="helloadmin_read_min" name="helloadmin_read_min" value="<?php echo esc_attr( $read_min ); ?>" min="1" max="60" style="width:80px">
</p>
<?php
}
// 3. Save
add_action( 'save_post', 'helloadmin_save_meta_box', 10, 2 );
function helloadmin_save_meta_box( $post_id, $post ) {
// The request came from our form
if ( ! isset( $_POST['helloadmin_meta_nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['helloadmin_meta_nonce'] ) ), 'helloadmin_save_meta' ) ) {
return;
}
// Not an autosave or a revision
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
return;
}
// The user may edit this post
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( isset( $_POST['helloadmin_subtitle'] ) ) {
update_post_meta( $post_id, '_helloadmin_subtitle', sanitize_text_field( wp_unslash( $_POST['helloadmin_subtitle'] ) ) );
}
if ( isset( $_POST['helloadmin_read_min'] ) ) {
update_post_meta( $post_id, '_helloadmin_read_min', min( 60, absint( $_POST['helloadmin_read_min'] ) ) );
}
}
The nonce protects from CSRF (a request sent from another site on behalf of a logged-in editor), and current_user_can() from users without rights. Both are needed, a nonce does not check rights.
In a template the values are read as usual:
<?php
$subtitle = get_post_meta( get_the_ID(), '_helloadmin_subtitle', true );
if ( $subtitle ) {
echo '<p class="subtitle">' . esc_html( $subtitle ) . '</p>';
}
Details
- A meta key starting with
_is hidden from the standard Custom Fields box, so editors do not see the field twice. - For one post type, use the hook
save_post_{post_type}(for examplesave_post_page): it runs only for that type. - If the values are needed in the REST API or in blocks, also register them with
register_post_meta( 'post', '_helloadmin_subtitle', array( 'show_in_rest' => true, 'single' => true, 'type' => 'string', 'auth_callback' => function () { return current_user_can( 'edit_posts' ); } ) ).
NOTE: In the block editor the post is saved through the REST API, and the meta boxes are sent in a separate request right after it, with their own $_POST. The handler above works in both editors, because everything it needs comes from that form.