I have been working on this solution, I have come up with a different idea to implement this –
I have added two different meta boxes, one with First Name, the other with Last Name
Then I have hooked a function with save_post_{$post->post_type}
hook like this –
add_action( "save_post_{$post->post_type}", "update_post_data" );
function update_post_data( $post_id ) {
// Unhook to stop infinite loop.
remove_action( "save_post_{$post->post_type}", "update_post_data" );
$fname = sanitize_title( get_post_meta( $post_id, '_first_name', true ) );
$lname = sanitize_title( get_post_meta( $post_id, '_last_name', true ) );
$post_title = ucwords( $fname . ' ' . $lname );
$post_name = $fname . '-' . $lname;
$args = [
'post_title' => $post_title,
'post_name' => $post_name,
];
// Update post data.
wp_update_post( $args );
// Re-hook the function, to make it work in future.
add_action( "save_post_{$post->post_type}", "update_post_data" );
}