Change WordPress user roles and capabilities › Forums › How to or FAQ › Gravity Forms User Registration
Tagged: gravity forms, user registration
- This topic has 11 replies, 3 voices, and was last updated 7 years, 11 months ago by cemfundog.
-
AuthorPosts
-
23/06/2015 at 15:32 #1556kardonParticipant
Can anyone tell me if they have connected URE with Gravity Forms User Registration add-on?
Specifically, I want to be able to set multiple roles on the user profile using a front end form.
25/06/2015 at 21:04 #1558kardonParticipantThe Gravity Forms team says they have never worked with URE but they have hooks that could be used for what we want to do if we know the actions to perform.
https://www.gravityhelp.com/documentation/category/user-registration/
Has the list of hooks.They believe the user updated one is what we need. https://www.gravityhelp.com/documentation/article/gform_user_updated/
Can you tell me what the PHP code would need to do in order for me to add a checkbox field for adding secondary roles?
26/06/2015 at 04:15 #1559VladimirKeymasterThanks for the additional information. There is no such feature currently. But I’m interested to realize it: 1st, as a PHP code. Then add it to the User Role Editor Pro.
Can you send me (support [at-sign] role-editor.com the installation package of “Gravity Forms User Registration” add-on? I will use it for the investigation/testing purpose and at my localhost only.
03/07/2015 at 04:23 #1576VladimirKeymasterCode:
// prefill roles at User profile editor Gravity Form fields add_filter( 'gform_pre_render_10', 'frontend_profile_populate_roles' ); function frontend_profile_populate_roles( $form ) { foreach ( $form['fields'] as &$field ) { if ( $field->type == 'select' && $field->inputName==='primary_role' ) { prepare_primary_role_dropdown($field); } if ( $field->type == 'multiselect' && $field->inputName==='secondary_roles' ) { prepare_secondary_roles_multiselect($field); } } // foreach return $form; } function get_primary_role() { global $current_user; if (!empty($current_user->roles)) { $roles = array_values($current_user->roles); $primary_role = array_shift($roles); } else { $primary_role = 'do-not-allow'; } return $primary_role; } function prepare_primary_role_dropdown( &$field ) { global $wp_roles; $primary_role = get_primary_role(); $choices = array(); foreach ($wp_roles->role_names as $key=>$value) { $is_selected = $primary_role===$key; $choices[] = array('text'=>$value, 'value'=>$key, 'isSelected'=>$is_selected, 'price'=>0.00); } $field->choices = $choices; } function prepare_secondary_roles_multiselect( &$field ) { global $current_user, $wp_roles; $primary_role = get_primary_role(); $choices = array(); foreach ($wp_roles->role_names as $key=>$value) { if ($key==$primary_role) { continue; } $is_selected = in_array($key, $current_user->roles); $choices[] = array('text'=>$value, 'value'=>$key, 'isSelected'=>$is_selected, 'price'=>0.00); } $field->choices = $choices; } // update user's roles according to the on form selection add_action( 'gform_user_updated', 'frontend_profile_change_user_roles', 10, 4); function frontend_profile_change_user_roles($user_id, $user_config, $entry, $user_pass) { $user = new WP_User($user_id); $user->set_role($entry[6]); // Primary role // secondary roles if (empty($entry[7])) { return; } $roles = explode(',', $entry[7]); foreach($roles as $role) { $user->add_role($role); } }
10 at the filter name ‘gform_pre_render_10’ is the ID of Gravity Forms form (with user profile fields including roles) linked to the “User Registration” add-on item with ‘update’ action.
Form ID=10 includes 2 roles related fields:
1) “Primary role” drop down field with “Allow field to be populated dynamically” flag turned on and “parameter name” value “primary_role”. It corresponds to the entry[6] at the code above.
2) “Secondary roles” multi select field with “Allow field to be populated dynamically” flag turned on and “parameter name” value “secondary_roles”. It corresponds to the entry[7] at the code above.14/07/2015 at 15:48 #1582kardonParticipantThank you so much for the work you did here!! Got this thing working pretty easily and it works great on all our testing. Rolling it out live in the next week!
15/07/2015 at 02:08 #1583VladimirKeymasterExcellent! Thanks for the feedback.
18/11/2016 at 22:11 #2973cemfundogParticipantHello Vladimir. My name is Brian. I just purchased this plugin due to this post. I am hoping that you can help me out a bit here. I am trying to register new users with 2 roles. One based on the predefined role in the gravity forms user registration addon, and the other from a radio button list on the same form. Due to a plugin that we had custom made for this project, I need each user to have a custom user role called ‘shiftee_customer’, and I also need them to have another role. So in other words, they can not share capabilities, they need to have 2 separate roles attached to their account after sign up. The code you provided is very close to what I need. Would you be willing to help me finish it up for my needs. I really appreciate your plugin and your help. Thank you in advance for looking.
20/11/2016 at 01:56 #2977VladimirKeymasterHi Brian,
Show a screenshot of your GF user registration add-on form.
I have Gravity Forms User Registration Add-On copy version 1.6 for testing. If you use another version send its copy to support [at-sign] role-editor.com
I will try to reproduce your form and write a script for it.I use a copy of commercial products provided by clients at the local development environment and for testing purpose only.
27/11/2016 at 21:46 #2995cemfundogParticipantDid you get my email with the attachment?
28/11/2016 at 10:54 #2997VladimirKeymasterHi Brian,
Thanks for this checking. I just found your message at the Gmail SPAM box. I will work on the subject. Excuse me for this delay with response.
29/11/2016 at 04:49 #2998VladimirKeymasterHi Brian,
I made an assumption that user select a secondary role from the business type radio list. So I added role IDs as the option values for its options: hospice, hospital, surgury_center.
You have to create roles with the same IDs. This interface element is available as the $entry[11] at the GF ‘gform_user_registered’ filter. So PHP code will be:add_action( 'gform_user_registered', 'frontend_register_add_2nd_user_role', 10, 4); function frontend_register_add_2nd_user_role($user_id, $user_config, $entry, $user_pass) { global $wp_roles; $user = new WP_User($user_id); $role = $entry[11]; // secondary role if (empty($role)) { return; } if (!isset($wp_roles->roles[$role])) { return; } $user->add_role($role); }
29/11/2016 at 05:01 #2999cemfundogParticipantI am so grateful for your help. This worked perfectly. You are the best. I will spread the word for your plugin and thank you so much for the extra, above and beyond, help.
-
AuthorPosts
- You must be logged in to reply to this topic.