Skip Navigation

[Resolved] Updating user role when submitting a form

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

Problem:
The issue here is that the user wanted to change their user role when a post form has been submitted.

Solution:

This can be done by using the following hook.

add_action('cred_save_data', 'change_user_role_on_save',10,2);
function change_user_role_on_save($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==573)
    {
     $user = wp_get_current_user();
   
       
if ( in_array( 'subscriber', (array) $user->roles ) ) {
         
// Remove role
$user->remove_role( 'subscriber' );
  
// Add role
$user->set_role('editor');
}
          
    }
}

What you need to do is replace 'subscriber' with the user's current role and then 'editor' with the role that you want the user to gain. Finally just change the 573 to the ID of the post form.

This code can be added to the Toolset custom code snippets at Toolset -> Settings -> Custom Code.

Once it has been added please ensure that you've activated it.

This support ticket is created 3 years, 6 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 8 replies, has 3 voices.

Last updated by joannaH 3 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#2059791

Is there a way to create a form that when submitted, the user role will be changed to something else? For instance, we want to setup up our users to initially be 'Registered' when they create an account. Then, once they submit a directory post, then we want the submission to trigger a role change to 'Active'. Is this possible? I do not see anywhere on the form creation to input this.

Thank you

#2060137

Shane
Supporter

Languages: English (English )

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

Hi Joanna,

Thank you for getting in touch. You should be able to achieve this using the Hook below.

What this does is to remove the registered role from the user and assigns them a new role of active. You will need to add the code below to your custom code section in Toolset -> Settings -> Custom Code and activate it. However you will need to change the 12 to the ID of the form that this should target.

add_action('cred_save_data', 'change_user_role_on_save',10,2);
function change_user_role_on_save($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
     $user = wp_get_current_user();
if ( in_array( 'registered', (array) $user->roles ) ) {
    
// Remove role
$user->remove_role( 'registered' );

// Add role
$user->add_role( 'active' );
}
        
    }
}
 

Please let me know if this helps.
Thanks,
Shane

#2063437

Thank you for this, my form ID is 300 and I replaced 'registered' with 'Contributor' in both instances and replaced 'active' with Active LC' and then added to custom code and activated. I created a user with role of 'Contributor', submitted form 300 and the role is not changing. Any ideas of what might be missing?

#2063497

Shane
Supporter

Languages: English (English )

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

Hi Joanna,

I've made a change to the code and this should get it working.

add_action('cred_save_data', 'change_user_role_on_save',10,2);
function change_user_role_on_save($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==573)
    {
     $user = wp_get_current_user();
  
      
if ( in_array( 'subscriber', (array) $user->roles ) ) {
        
// Remove role
$user->remove_role( 'subscriber' );
 
// Add role
$user->set_role('editor');
}
         
    }
}

As per usual replace the 573 with the ID of the form as well as replace subscriber and editor with the respective roles that you want to switch from.

Also once you've added the code to the custom code section, ensure that you've activated it.

Thanks,
Shane

#2064423

I updated the custom code and acitvated it. Still not getting the role to change,

add_action('cred_save_data', 'change_user_role_on_save',10,2);
function change_user_role_on_save($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==300)
{
$user = wp_get_current_user();

if ( in_array( 'Contributor', (array) $user->roles ) ) {

// Remove role
$user->remove_role( 'Contributor' );

// Add role
$user->set_role('Active LC');
}

}
}

#2064559

Shane
Supporter

Languages: English (English )

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

Hi Joanna,

Could you provide me with admin access to the site so that I can have a further look at this for you ?

Also please send me the link to the form as well.

Thanks,
Shane

#2066563

Hello, Shane is out today for a national holiday, and will return tomorrow to continue assisting you. Thanks for your patience.

#2067759

Shane
Supporter

Languages: English (English )

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

Hi Joanna,

This should now be working.

Please double check on your end as I've resolved the issue.

Thanks,
Shane

#2067983

My issue is resolved now. Thank you!