Skip Navigation

[Resolved] User Form -> Create new user – > add Role

This thread is resolved. Here is a description of the problem and solution.

Problem: How to change the role of the users after creating the user using Toolset User form

Solution: You can use the Toolset Forms hook "cred_save_data" to change the user role.

You can find proposed solution in this case with the following reply: https://toolset.com/forums/topic/user-form-create-new-user-add-role/#post-2042379

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.

This topic contains 4 replies, has 2 voices.

Last updated by stefanK-7 1 month, 2 weeks ago.

Assigned support staff: Minesh.

Author
Posts
#2041869
Bildschirmfoto 2021-05-04 um 08.39.13.png

In an user form I need to add a role based on a checkbox. I added a checkbox with field slug file-supplier to the user form. I wrote the code to added the role based on this thread: https://toolset.com/forums/topic/unable-to-change-user-role-from-cred/

Here is my code:

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

function cred_add_user_role($post_id, $form_data) {
// if a specific form
if ($form_data['id'] == 12)
{
// error_log("_POST");
// error_log(print_r($_POST, true));
if (isset($_POST['file-supplier']))
{
error_log("file-supplier");
$user = new WP_User( $post_id );
$user->add_role('Author');
}
}
}

The code is called, I checked the error_log.

When I check the new user created, there is only the role selected in the user form added. Not the role added by code.

How do I properly add another role based on a checkbox?

#2042379

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

It seems - the role name you have to use is in small letters.

Can you please try to use the following code:

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

function cred_add_user_role($post_id, $form_data) {
// if a specific form
if ($form_data['id'] == 12)
{
// error_log("_POST");
// error_log(print_r($_POST, true));
if (isset($_POST['file-supplier']))
{
error_log("file-supplier");
$user = new WP_User( $post_id );
$user->add_role('author');
}
}
}

Do you see it working?

#2042389
Bildschirmfoto 2021-05-04 um 14.01.05.png

Yes, this is working, but what will I use for a Custom Role "File Supplier"? I already tried $user->add_role('File Supplier'); and $user->add_role('file supplier'); I added 3 new Custom Roles and based on a checkbox in the user form I need to add the file supplier role.

#2042399

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You can use it as given under:

$user->add_role('file_supplier'); 
#2042409

My issue is resolved now. Thank you!