OK, I am assuming you are talking about adding a meta box, similar to Yoast SEO’s meta box… if that IS the case, then try this:
//First, we add all the metaboxes we need...
//You can just repeat the `add_meta_box()` for each one
function add_yournew_metaboxes() {
add_meta_box(
'your_metabox_id',
'The Title',
'your_metabox_callback', //the next function below
array(
'post',
'page'
),
'normal',
'high',
array(
'__block_editor_compatible_meta_box' => true,
'__back_compat_meta_box' => false,
)
);
}
//When we add a metabox we specify a callback function
//The third line in adding your metabox should match the name of this function
function your_metabox_callback( $post ) {
//we want a unique nonce for security and to ensure only this post/page is updated
wp_nonce_field( basename( __FILE__ ), 'yourmetabox_nonce' );
//put your metabox content in here
}
//When a user clicks 'Update/Publish', we want to make sure we're saving any data in the metaboxes
//This data will be saved as `post_meta`
function save_your_metabox_data( $post_id ) {
//you'll have to run through any fields you put in the metabox and save them
if( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
if( !isset( $_POST['yourmetabox_nonce'] ) || !wp_verify_nonce( $_POST['yourmetabox_nonce'], basename( __FILE__ ) ) ) {
return $post_id;
}
//loop through all of your fields
$yourmetabox_meta['one_of_your_fields'] = esc_textarea( $_POST['one_of_your_fields'] );
$yourmetabox_meta['another_field'] = esc_textarea( $_POST['another_field'] );
foreach( $yourmetabox_meta as $key => $value ) :
if( 'revision' === $post->post_type ) {
return;
}
if( get_post_meta( $post_id, $key, false ) ) {
update_post_meta( $post_id, $key, $value );
} else {
add_post_meta( $post_id, $key, $value);
}
if( !$value ) {
delete_post_meta( $post_id, $key );
}
endforeach;
}
add_action( 'save_post', 'save_your_metabox_data' );