Change WordPress user roles and capabilities › Forums › Give user access to plugin – how to › Multiple roles help
- This topic has 3 replies, 3 voices, and was last updated 6 years, 7 months ago by Vladimir.
-
AuthorPosts
-
13/03/2016 at 13:40 #2120dweb360Participant
Hello,
I have bbpress forum installed and each user have 2 roles by default :
Role : pending
Forum role : participant
Users are registred via Formidable pro forms
I have a dropdown field with statues, if ‘Complet ‘statut is selected thant the user will have theses new roles :
Role : instructor
Forum role : participantPlese help me to modifiy thsi code to be able to change the main role from ‘pending’ to ‘instructor’ when the statut in dropdown list change to ‘Complete’
add_action('frm_after_update_entry', 'after_entry_updated', 10, 2); function after_entry_updated($entry_id, $form_id){ if($form_id == 6){ if($_POST['item_meta'][158] == 'Complet'){ $mon_user_id = $_POST['item_meta'][151]; $user_data_info = get_userdata( $mon_user_id ); $user_roles = $user_data_info->roles; $user_role = array_shift($user_roles); if ( $user_role == 'pending' ) { $user = new WP_User( $mon_user_id ); $user->remove_role( 'pending' ); $user->add_role('instructor'); } } } }
Thank you in advance
13/03/2016 at 16:18 #2124VladimirKeymasterHi,
1st, correct the typo, you should have ‘Complete’ instead of ‘Complet’ in the 2nd login expression.
2nd, role shown by WordPress as a ‘primary’ role should be the 1st in the roles array (have index 0). So remove all roles from the user, then add ‘instructor’ role. It will become a primary this way. Then add ‘participant’ role back as a secondary role.
$user->set_role('instructor'); $user->add_role('participant');
13/04/2018 at 22:01 #4780siadiciccoSpectatorI am trying to remove and add a (or many) role on a form update and I am at a road block:
Do you have any insight?add_action(‘frm_after_create_entry’, ‘newweb_to_website’, 20, 2);
function newweb_to_website($entry_id, $form_id){
if($form_id == 41){
if ( $user_role == ‘customer’ ) {
$user->remove_role( ‘new-web’ );
$user->add_role(‘website’);
}$user = wp_get_current_user();
if(!$user) {
return;
}$updated_user = (array)$user;
// Get the highest/primary role for this user
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
if ( $user_role == ‘administrator’ )
return; //make sure we don’t downgrade any admins$updated_user[‘role’] = $new_role;
wp_update_user($updated_user);
}
}14/04/2018 at 03:27 #4781VladimirKeymaster1st, $user_role and $user variables are not defined in these code lines:
if ( $user_role == ‘customer’ ) { $user->remove_role( ‘new-web’ );
You should start from getting current user:
$user = wp_get_current_user(); if(!$user) { return; }
Then check if current user is not administrator.
Then make manipulations with user role.
If you use $user->add_role(), $user->remove_role(), there is no need to use wp_update_user(). -
AuthorPosts
- You must be logged in to reply to this topic.