Given that you will essentially make the first and last name required then it won't be possible to create a user without them entering their first and last name since the form validation won't work.
I've made an update to the code.
add_action('cred_save_data', 'save_username',10,2);
function save_username($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==28)
{
$firstname = $_GET['first_name'];
$lastname = $_GET['last_name'];
$user_name = $firstname+$lastname;
wp_update_user(array( 'ID' => $post_id, 'user_login' => $user_name ));
}
}
Also please remember to create the custom validation for the first and last name.
If this still doesn't work please let me know and send me a link to the page where the registration form is.
The code is still not working and I think it might be because I'm editing the form in "Expert mode" because I have specific conditional statements to add that don't work in GUI mode.
Is there a way to make the username autogenerate with the form editing in "Expert mode"?
Also, I am not sure how to create the custom validation.
I've made some more adjustments to the code. Can you update the code to
add_action('cred_save_data', 'save_username',10,2);
function save_username($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==28)
{
$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$user_name = $firstname.''.$lastname;
global $wpdb;
$wpdb->update($wpdb->users, array('user_login' =>$user_name ), array('ID' => $post_id));
}
}
Also can you remove the test users that I made here. hidden link
Here is the code below to make both fields required.
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
//uncomment this if you want to print the field values
//print_r($fields);
//validate if specific form
if ($form_data['id']==28)
{
//check the first name value
if (empty($fields['first_name']['value']))
{
//set error message for first name
$errors['my_field']='Please enter your first name';
}
//check the last name value
if (empty($fields['last_name']['value']))
{
//set error message for last name
$errors['_featured_image'] = 'Please enter your last name';
}
}
//return result
return array($fields,$errors);
}
Please try this function and let me know if it helps.
Thanks,
Shane