Skip Navigation

[Resolved] Choose user role on registration

This support ticket is created 7 years, 11 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 3 voices.

Last updated by davidN-5 7 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#402779

I'm trying to create a user registration form using the example provided here:
https://toolset.com/forums/topic/allow-users-to-select-a-role-when-registeringediting-profile-with-cred-beta-3/

I created a user field, called profession, for selecting the role. This is my code:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
    if ($form_data['id']==194) {
        if (isset($_POST['profession'])) {
            $u = new WP_User($post_id);
            $u->set_role($_POST['profession']);
        }
    }
}

In the end the role is not updated.

Am I missing anything?

Thank you.

#402885

Shane
Supporter

Languages: English (English )

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

Hi Valeria,

I'm assuming that profession is a custom field on your form to select the user role correct?

If so then the custom field should be named wpcf_professions . If that does not work could you let me have a look at the form so I can see the appropriate name for the form field.

Thanks,
Shane

#402966

Hi, correct.

So my final code is

add_action('cred_save_data', 'my_save_data_action',10,2);

function my_save_data_action($post_id, $form_data) {
    if ($form_data['id']==199) {
        if (isset($_POST['wpcf_profession'])) {
            $u = new WP_User($post_id);
            $u->set_role($_POST['wpcf_profession']);
        }
    }
}

And the form is:

[creduserform class='cred-user-form cred-keep-original']
	[cred_field field='form_messages' value='']

	<div class="cred-field cred-field-profession">
		<label class="cred-label">Profession</label>
		[cred_field field='profession' post='user' value='' urlparam='']
	</div>
	<div class="cred-field cred-field-first_name">
		<label class="cred-label">Name</label>
		[cred_field field='first_name' post='user' value='' urlparam='']
	</div>
	<div class="cred-field cred-field-user_email">
		<label class="cred-label">Email</label>
		[cred_field field='user_email' post='user' value='' urlparam='']
	</div>
	<div class="cred-field cred-field-phone-no">
		<label class="cred-label">Phone No</label>
		[cred_field field='phone-no' post='user' value='' urlparam='']
	</div>

	[cred_field field='form_submit' value='Submit' urlparam='']
[/creduserform]

It still doesn't work.

It's a multisite, does it change anything?

Thank you.

#402998

I also need a function that allows to change user role on front-end. Is it the same as this one, but with it's own form id?

Thank you.

#403124

Shane
Supporter

Languages: English (English )

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

Hi Valeria,

Yes this is the same however if your create the roles using ACCESS then when you setup a roles custom field for your form, the slug of the roles must be the same as value field of your custom fields in order for it to work.

So far you have things setup correctly , all you need to do is to ensure that your slugs match.

Thanks,
Shane

#403225
s2016-06-01 (4).png
s2016-06-01 (2).png
s2016-06-01.png

It's still not working for me.

At the moment I'm trying to edit user role from the front-end.

I attached all the screenshots. Could you have a look again please?

#403414

Shane
Supporter

Languages: English (English )

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

Hi Valeria,

Would you mind providing me with admin access to the site so I can have a look at the form?

The private fields will be enabled for your next response.

Thanks,
Shane

#403886

Shane
Supporter

Languages: English (English )

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

Hi Valeria,

I took a look at it but there was an error in my PHP code so the site is down at the moment 🙁

Could you check on the FTP details for me so that I can log in and edit the files on the server to restore the test site ?

Looking forward to hearing from you soon.

Thanks,
Shane

#403995

I did send you the FTP details. They are correct, I've jut checked.

In case you can't login via FTP due to server response, try SFTP please, I enabled SSH for the next 24 hours.

Thank you.

#404194

Shane
Supporter

Languages: English (English )

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

Hi Valeria,

I made a few changes to the code.

The correct code should be.

add_action('cred_save_data', 'my_save_data_action',10,2);

function my_save_data_action($post_id, $form_data) {
    if ($form_data['id']==5) {
        if (isset($_POST['wpcf-profession'])) {
            $u = new WP_User($post_id);
        	$u->set_role($_POST['wpcf-profession']);
        }
    }
}

I've tested it and it's working fine now.

Please let me know if this helps.
Thanks,
Shane

#404575

Hi Shane,

Thanks a lot for your help. Working perfectly.

#1572563

Hello,

I'm also following along with the instructions in this post and here: https://toolset.com/forums/topic/allow-users-to-select-a-role-when-registeringediting-profile-with-cred-beta-3/

However, when my new user is created, they are given a role of 'none.' I believe I'm using the same slug for both the custom field role values and for the User Role values created in Access. One thing to note is that I am using Checkboxes as the field type. Please advise.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.