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" );
}
https://example.com/first-last/
(no base), or is it likehttps://example.com/authors/first-last/
(base isauthors
)? What are these authors - authors of what? And as for splitting the post title, are you sure the author's name will always befirst last
and notlast first
orfirst middle last
?John Doe
, then WordPress will usejohn-doe
as the post slug that's used in the permalink. As for creating theauthors
post type, you can useregister_post_type()
, and there are generators like this that you can quickly try. As for the first/last names, you can add them either as custom fields or terms in a custom taxonomy.