form – create profile post on user register

With Types it’s possible to add User Custom Fields, this is very useful to extend WP User’s attributes, but in certain cases it is better to work with a standard Custom Post Type (CPT) to create a real “User Profile”. This is because a standard CPT is more flexible, it works better with Views and you can connect it with other CPTs by Toolset relationships.

For example, it’s possible to use the authorship of posts to group them because each post is connected to a user author (ex: articles written by a certain WP User can be filtered by author), but if it is needed to create a many-to-many relationship between posts and authors (ex: an author can write many articles, but an article can be also written by many authors) it is necessary to create a User Profile CPT.

The architecture is simple, each WP User is author of a User Profile CPT post. The User Profile post can be connected to any post of any other CPT.

User registration flow would be: the user register a new WP User by a Toolset User Form, then the logged in user has to create the User Profile post by a Toolset Form.

It is possible to make this process smoother by creating the User Profile post automatically on user registration.

I can do this with this function called by a cred_save_data action:

add_action('cred_save_data', 'my_save_data',10,2);
function my_save_data($post_id, $form_data) {

  // User - Register - FORM
  if ($form_data['id']==55) {

    $user_info = get_userdata($post_id);

    // User profile CPT add
    $user_profile_id = wp_insert_post(array (
      'post_type' => 'user-profile',
      'post_title' => $user_info->display_name,
      'post_name' => $user_info->user_login,
      'post_author' => $user_info->ID,
      'post_content' => '',
      'post_status' => 'publish',
      'comment_status' => 'closed',
      'ping_status' => 'closed',
    ));
  }
}


get_userdata() reference: https://developer.wordpress.org/reference/functions/get_userdata/
wp_insert_post() reference: https://developer.wordpress.org/reference/functions/wp_insert_post/

The function retrieves all the user’s data by get_userdata($post_id) (where $post_id is the WP User ID created by the Toolset User Form), then use the information to create a new post by wp_insert_post() functions. The function assigns the post to user-profile CPT and it uses: the display name as post Title, the login name as Slug and the user ID as Author.

Remember to change “$form_data[‘id’] == 55” with your Toolset User Form ID and “‘post_type’ => ‘user-profile'” with the slug of your User Profile CPT.

It is possible to combine this script with snippet.