Skip Navigation

[Resuelto] Auto generate username from form fields

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

I have a user registration form, I'd like the generated username to be in the form: company-firstname-lastname

Solution:

It needs custom codes, see example here:

https://toolset.com/forums/topic/auto-generate-username-from-form-fields/#post-1258333

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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.

Our next available supporter will start replying to tickets in about 5.65 hours from now. Thank you for your understanding.

This topic contains 9 respuestas, has 2 mensajes.

Last updated by Tim Elliott hace 2 años, 6 meses.

Assigned support staff: Luo Yang.

Autor
Mensajes
#1256469

Hi Support Team,

I have a user registration form:
hidden link

I'd like the username it generates to be a specific format. Other fields in the form are:
Email
First Name
Last Name
Company

I'd like the generated username to be in the form:
company-firstname-lastname

Is this possible using cred forms?

I've tried to use jQuery in the form as follows:

jQuery(document).ready(function(){
    var company = document.getElementById("cred_user_form_48232_2-textfield-5-1559428536").value;        
    var fname   = document.getElementById("cred_user_form_48232_2-textfield-3-1559428536").value;
    var lname   = document.getElementById("cred_user_form_48232_2-textfield-4-1559428536").value;
    document.getElementById("cred_user_form_48232_2-textfield-1-1559428536").value = company + "-" + fname + "-" + lname ;

    jQuery("#cred_user_form_48232_2-textfield-5-1559428536, #cred_user_form_48232_2-textfield-3-1559428536, #cred_user_form_48232_2-textfield-4-1559428536").change(function(){
        company = document.getElementById("cred_user_form_48232_2-textfield-5-1559428536").value;        
        fname   = document.getElementById("cred_user_form_48232_2-textfield-3-1559428536").value;
        lname   = document.getElementById("cred_user_form_48232_2-textfield-4-1559428536").value;
        document.getElementById("cred_user_form_48232_2-textfield-1-1559428536").value = company + "-" + fname + "-" + lname ;
  });
});

But
1 - this causes other jQuery items on the page not to load
2 - the last part of the id (1559428536) changes every time I save the form so the id isn't ever right.

If you have a way to make this work it would be much appreciated.

Thanks
Tim

#1257477

I've received an notification of a reply by email, but the reply isn't showing up in here. I can follow use the email for my solution but it seems like there's a bug in the support system you might want to look at.

#1257819

Luo Yang
Supporter

Languages: Inglés (English ) Chino simplificado (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Hello,

I remember I have answered this ticket yesterday:
Using Form API action hook cred_save_data to update the user's login name with PHP codes.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://wordpress.stackexchange.com/questions/103504/how-to-programatically-change-username-user-login

It seems to be a mistake that the answer was trashed and lost, sorry for the inconvenience, please let me know if you need more assistance for it. thanks

#1257949

Thanks for your help. FYI your original reply was:

In your case, it needs custom codes, for example:
1) When user submit the user form for creating user, you can use action hook cred_save_data to trigger a custom PHP function

2) In this PHP function, get all those three custom user field values:
- Email
- First Name
- Last Name
- Company
https://developer.wordpress.org/reference/functions/get_user_meta/

Generate the new user username, and update the database:
https://codex.wordpress.org/Function_Reference/wp_update_user
user_login A string that contains the user's username for logging in. Please note that the function cannot alter this field, since WordPress does not allow usernames to be changed.

#1257959

Luo Yang
Supporter

Languages: Inglés (English ) Chino simplificado (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

There is a mistake in above answer, you need to setup custom PHP codes to update the user login name, see the thread link I mentioned above:
https://wordpress.stackexchange.com/questions/103504/how-to-programatically-change-username-user-login

For your reference.

#1257961

I've tried to make this update the nickname as follows:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($new_user_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==48232)
    {
		$usercompany = get_post_meta($new_user_id, 'wpcf-user-company', true);
        if(isset($_POST['first_name']) && isset($_POST['last_name'])){
            update_user_meta( $new_user_id, 'nickname', $usercompany."-".$_POST['first_name']."-".$_POST['last_name']);
        }
    }
}

The usercompany is not coming through into the nickname though so I end up with -firstname-lastname
I guess there's something wrong with my get_post_meta line.

My other problem is that I wanted to make this update the username but the WordPress codex says:
user_login A string that contains the user's username for logging in. Please note that the function (wp_update_user) cannot alter this field, since WordPress does not allow usernames to be changed.

That was one reason I was trying to do it with jQuery on the frontend prior to form submission.

I might be able to utilize the nickname instead but would prefer to set the username if possible.

#1257975

Right, I was still writing my answer when you answered the previous...
So I see that should be a solution to updating the username - I'll give it a try.

So can you just help on the reason the user_company field isn't getting passed into the $usercompany variable.

#1257977

It's going to be user get_user_meta instead of get_post_meta isn't it...

#1257983

Luo Yang
Supporter

Languages: Inglés (English ) Chino simplificado (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Yes, you are right, since it is a WordPress user field, you need to use get_user_meta function:
https://developer.wordpress.org/reference/functions/get_user_meta/

#1258333

My issue is resolved now. Thanks for your help.

The final function I now have in my child theme functions.php is

/* Registration form - Set the Username and Nickname to company-firstname-lastname */
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($new_user_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==48232)
    {
        if(isset($_POST['first_name']) && isset($_POST['last_name'])){
			$first_name = sanitize_text_field($_POST['first_name']);
			$last_name = sanitize_text_field($_POST['last_name']);
			$usercompany = sanitize_text_field(get_user_meta($new_user_id, 'wpcf-user-company', true));
			
            $my_nickname = $usercompany . "-" . $first_name . "-" . $last_name;
			$my_username = strtolower($my_nickname);
			
			update_user_meta( $new_user_id, 'nickname', $my_nickname);
			global $wpdb;
			$wpdb->update($wpdb->users, array('user_login' => $my_username), array('ID' => $new_user_id));
        }
    }
}