Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
How do I display custom post title as "first_name last_name"

I have created a site whereby athletes can register and create their own templated profile page. On registration, I automatically create a custom post (their profile) and set the user as the author of that post.

However, I cannot for the life of me set the post title as the user's (now author's) first name and surname. For instance, if John Smith registers, I would like the post title to read John Smith, and the slug to convert to athlete/john.smith.

I have used $user_info->nickname for now, but this causes both the title and the slug to read as john.smith

This is the code I am using -any pointers would be greatly appreciated:

add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );

function wpse_216921_company_cpt( $user_id )
{
    // Get user info
    $user_info = get_userdata( $user_id );
    $user_roles = $user_info->roles;

    // New code added 
    $this_user_role = implode(', ', $user_roles );

    if ($this_user_role == 'author') {

        // Create a new post
        $user_post = array(
            'post_title'   => $user_info->nickname,
            'post_status'  => 'publish', // <- here is to publish
            'post_type'    => 'athlete', // <- change to your cpt
            'post_author'  => $user_info->ID
        );
        // Insert the post into the database
        $post_id = wp_insert_post( $user_post );
    }
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You should also use post_name for slug(john.smith) and first_name,last name for post title like this:

add_action( 'user_register', 'wpse_216921_company_cpt', 20, 1 );

function wpse_216921_company_cpt( $user_id )
{
    // Get user info
    $user_info = get_userdata( $user_id );
    $user_roles = $user_info->roles;

    // New code added 
    $this_user_role = implode(', ', $user_roles );

    if ($this_user_role == 'author') {
        
        $post_title = $user_info->first_name.' '.$user_info->last_name;
        $post_title = trim(ucwords($post_title));
        $post_slug  = preg_replace('/\s+/', '.', $post_title);

        // Create a new post
        $user_post = array(
            'post_title'   => $post_title, //$user_info->display_name,
            'post_name'    => $post_slug,
            'post_status'  => 'publish', // <- here is to publish
            'post_type'    => 'athlete', // <- change to your cpt
            'post_author'  => $user_info->ID
        );
        // Insert the post into the database
        $post_id = wp_insert_post( $user_post );
    }
}
over 4 years ago · Santiago Trujillo Report

0

You can use firstname and lastname like below:

add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );

function wpse_216921_company_cpt( $user_id )
{
    // Get user info
    $user_info = get_userdata( $user_id );
    $user_roles = $user_info->roles;

    // New code added 
    $this_user_role = implode(', ', $user_roles );

    $first_name = $user_info->first_name;
    $last_name = $user_info->last_name;

    if ($this_user_role == 'author') {

        // Create a new post
        $user_post = array(
            'post_title'   => $first_name . ' ' . $last_name,
            'post_status'  => 'publish', // <- here is to publish
            'post_type'    => 'athlete', // <- change to your cpt
            'post_author'  => $user_info->ID
        );
        // Insert the post into the database
        $post_id = wp_insert_post( $user_post );
    }
}

Not tested but it should works.

over 4 years ago · Santiago Trujillo Report

0

Thanks for the assistance guys. It turned out to be an Ultimate Member issue, and I needed to use the UM Hook um_registration_complete.

The final solution that worked is as follows:

//Create Custom Post Type on registration

add_action( 'um_registration_complete', 'wpse_216921_company_cpt', 10, 1 );
function wpse_216921_company_cpt( $user_id )
{
   // Get user info
   $user_info = get_userdata( $user_id );
   $user_roles = $user_info->roles;

   // New code added
   $this_user_role = implode(', ', $user_roles );

   if ($this_user_role == 'author') {

   $post_title = $user_info->first_name.' '.$user_info->last_name;
   $post_title = trim(ucwords($post_title));
   $post_slug = preg_replace('/\s+/', '.', $post_title);

   // Create a new post
   $user_post = array(
   'post_title' => $post_title, //$user_info->display_name,
   'post_name' => $post_slug,
   'post_status' => 'publish', // <- here is to publish
   'post_type' => 'athlete', // <- change to your cpt
   'post_author' => $user_info->ID
   );
   // Insert the post into the database
   $post_id = wp_insert_post( $user_post );
   }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!