Problem: I would like to modify the roles associated with a new User when a Form is submitted.
Solution:
Use the cred_save_data API to modify the User's roles.
add_action('cred_save_data', 'my_save_data_action',10,2); function my_save_data_action($post_id, $form_data) { // if a specific form if ($form_data['id']==290) { $current_user_id = $post_id; $current_user_data = get_userdata( $post_id ); $oldroles = $current_user_data->roles; //here should be just one, as it is the first time you edit this user and it is only one-role if( !is_array($oldroles) ) { $oldroles = array($oldroles); } //Add new role after you remove old role $u = new WP_User( $post_id ); foreach( $oldroles as $oldrole ) { $u->remove_role( $oldrole ); } $newrole = $_REQUEST['user_select_role']; $u->add_role( $newrole ); } }
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.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 10 replies, has 3 voices.
Last updated by 6 years, 4 months ago.
Assisted by: Christian Cox.