Skip Navigation

[Resolved] Save username as user's First and Last name and make fields required

This support ticket is created 3 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 11 replies, has 2 voices.

Last updated by Arrien 3 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#2163023

I have built a user form where I would like the autogenerated username to be the user's first and last name.

I also want to make sure that the First and Last name fields are required.

Thank you

#2163115

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Arrien,

Thank you for getting in touch.

To change the username you're going to have to Hook into the form and update the user fields .

Try using the function below.


add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
          $firstname = $_GET['first_name'];
          $lastname = $_GET['last_name'];
          $user_name = $firstname+$lastname;
            wp_update_user(array( 'ID' => $post_id, 'username' => $user_name ));
        }
    }
}

Replace 12 with ID of your user form.

Secondly you can use the hook below to do a custom validation on the form for the first and last name.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Please let me know if this helps.
Thanks,
Shane

#2163145

Hi Shane,

It doesn't seem to be working.

I've setup the function as a plug-in:

<?php
/*
Plugin Name: Save username
Description: Save username using First Name and Last Name
*/

//Save username using First Name and Last Name

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, 'username' => $user_name ));
}
}

What happens if I don't have the First name and last name required, what would the username save as?

Thank you,
Arrien

#2163561

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Arrien,

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.

Thanks,
Shane

#2163599

Hi Shane,

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.

Thanks,
Arrien

#2163767

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Arrien,

This shouldn't matter in a case like this.

Would you mind allowing me to have admin access to the site so that I can have a look ?

I've enabled the private fields for your next response.

Thanks,
Shane

#2164733

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Arrien,

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

Thanks,
Shane

#2164775

Great! That works really well!

Now, I am really not sure how to make the First and Last name fields required.

Thanks,
Arrien

#2164909

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Arrien,

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

#2165053

Hi Shane,

That also worked quite well.

Is there a way to add a clear form fields button so that a person filling out the form can reset it if they've made a mistake?

Thanks,
Arrien

#2165131

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Arrien,

Unfortunately no that won't be possible to do without some amount of custom code as we don't have a hook that can provide this functionality.

Thanks,
Shane

#2165847

My issue is resolved now. Thank you!