Skip Navigation

[Resolved] Change User Role with Toolset Form

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

Problem:
Trying to change the user role from pending to approved provoked a fatal error on the site.
Solution:
I changed the code and switched how the user role is added. I initiate a variable with the user's ID, remove the old role and add the new one as follow:

add_action('cred_submit_complete', 'cred_update_user_role_pending', 10,2);
  
function cred_update_user_role_pending($post_id, $form_data) {
 require_once(ABSPATH.'wp-admin/includes/user.php');
 
  if ( isset($form_data) && !empty($form_data) && isset($form_data['id']) && $form_data['id'] == 4981 ) {  
    // update the user role 
     
    $user = new WP_User( $post_id );
    $user->remove_role( 'pending_vendor' );
    $user->add_role( 'vendor' );
     
  }
}

I also put in a check to make sure by the time the form is submitted, there's a valid formdata, it was causing a fatal error for some reason.

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 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 3 replies, has 2 voices.

Last updated by Mateus Getulio 8 months, 2 weeks ago.

Assisted by: Mateus Getulio.

Author
Posts
#2686102

Ian

I am creating an approval form that changes a user role from "pending-vendor" to "vendor"

I am following the instructions here: https://toolset.com/forums/topic/admin-change-user-role-from-front-end-form-submission/

My form is in a loop and I am calling my form with this shortcode
[cred_user_form form='approve-vendor-application' user='[wpv-user field="ID"]']

I have added the following code:
add_action('cred_save_data', 'cred_update_user_role_pending',10,2);

function cred_update_user_role_pending($post_id, $form_data) {
require_once(ABSPATH.'wp-admin/includes/user.php');

if ($form_data['id'] == '4981') {
// update the user role
wp_update_user ( array ( $post_id, 'role' => 'vendor' ));
}
}

Unfortunately my user role does not change when the form is submitted.

#2686263

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

Thank you for your reply.

Similar to your other ticket: https://toolset.com/forums/topic/delete-user-from-form-not-working, my guess is that it could be some compatibility issue or maybe the code not being called at the right time by the plugin code-snippets.

Likewise the other ticket, just as a test, please temporarily remove the code from the plugin code-snippets and add it to your functions.php file to see if it gets the issue fixed.

If the issue remains, I would like to request temporary access (wp-admin and FTP) to your site to take better look at both issues. You will find the needed fields for this below the comment area when you log in to leave your next reply. The information you will enter is private which means only you and I can see and have access to it.

Our Debugging Procedures

I will be checking various settings in the backend to see if the issue can be resolved. Although I won't be making changes that affect the live site, it is still good practice to backup the site before providing us access. In the event that we do need to debug the site further, I will duplicate the site and work in a separate, local development environment to avoid affecting the live site.

Privacy and Security Policy

We have strict policies regarding privacy and access to your information. Please see:
https://toolset.com/purchase/support-policy/privacy-and-security-when-providing-debug-information-for-support/

**IMPORTANT**

- Please make a backup of site files and database before providing us access.
- If you do not see the wp-admin/FTP fields this means your post & website login details will be made PUBLIC. DO NOT post your website details unless you see the required wp-admin/FTP fields. If you do not, please ask me to enable the private box. The private box looks like this: hidden link

Please, let me know if you need any additional details. Have a nice day.

#2686545

Ian

I sent you all the private stuff in my reply to my other request.

Some links that may help.
Same page as the delete form: hidden link
Snippit is here: hidden link

Thanks

#2686563

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

I changed the code slightly in order to get it worked, this is the final version:

add_action('cred_submit_complete', 'cred_update_user_role_pending', 10,2);
 
function cred_update_user_role_pending($post_id, $form_data) {
 require_once(ABSPATH.'wp-admin/includes/user.php');

  if ( isset($form_data) && !empty($form_data) && isset($form_data['id']) && $form_data['id'] == 4981 ) {  
    // update the user role 
    
	$user = new WP_User( $post_id );
	$user->remove_role( 'pending_vendor' );
	$user->add_role( 'vendor' );
	
  }
}

I changed how the role was changed with an instantiated object approach and I added a check to see if the form_data is not null before checking the form ID just in case.

After that I tested the functionality and I was able to delete the test vendor just fine.

Please test it again and let us know the results.

Mateus

#2686700

Ian

Works great, I will take a closer look to see where I went wrong.