2

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 );
    }
}
Andrew
  • 65
  • 5

3 Answers3

2

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 );
    }
}
Rajeev Singh
  • 1,724
  • 1
  • 6
  • 23
  • Thanks Rajeev! I unfortunately still can't get it the title to display as [first_name.last_name] so I can't test if your post_slug snippet works (it looks good, so I'm sure it does). Could the problem be that the code is firing before the actual meta data is saved in my database, and is therefore not picking up the first_name and last_name meta data from the keys? I'm using an Ultimate Member registration form plugin, but the data is definitely sent to my DB when the user is created. – Andrew Oct 13 '21 at 11:36
  • Hi, If you want a Title like [first_name.last_name] then you should change/set/replace $post_title to $post_slug because currenlty your post title($post_title) is [first_name last_name] and post slug($post_slug) like [first_name.last_name], replace $post_title with $post_slug on $user_post array() like this : $user_post = array( 'post_title' => $post_slug , 'post_name' => $post_title ) and try this – Rajeev Singh Oct 14 '21 at 06:03
1

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.

  • Thanks so much for the speedy response @akhtarujjaman! That's what I initially thought would work, but the post title keeps being set as "Untitled"' when I use that code. I've checked the database, and the "first_name" and "last_name" meta_values are there, but for some reason they are not used to populate the title. – Andrew Oct 08 '21 at 12:24
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 );
   }
}
Andrew
  • 65
  • 5