Apply user roles upon registration
-
Hi,
I have a buddypress theme installed that allows to handle WPJM plugin for job and resume registrations. For this latter I need the users to register as candidate or employer to control who can view resumes or post jobs etc.
However the theme dont have this per standard but their support where very kind to give me a suggested custom code to handle this. I cant get this to work and wonder if someone knows what might be missing?
This is what they wrote me for this question:
Create a Profile Field for “Role”: Set options like “Employer” and “Candidate” for users to select during registration.
Use Code to Assign the Role: A code snippet can assign the correct role based on the user’s selection in this profile field.Please let us know if you’d like guidance on the custom code snippet to achieve this or if you’d prefer assistance from a developer.
Here is a code snippet you can use to assign a user role based on the value selected in a custom BuddyPress profile field during registration. This example assumes the profile field is named “Role” and has values such as “Employer” or “Candidate.”
function assign_role_based_on_profile_field($user_id) { // Get the BuddyPress profile data for the registered user if (function_exists('xprofile_get_field_data')) { $role_value = xprofile_get_field_data('Role', $user_id); // Replace 'Role' with the exact name of your profile field // Assign the user role based on the profile field value if ($role_value == 'Employer') { // Assign 'employer' role $user = new WP_User($user_id); $user->set_role('employer'); // Replace 'employer' with the exact role slug } elseif ($role_value == 'Candidate') { // Assign 'candidate' role $user = new WP_User($user_id); $user->set_role('candidate'); // Replace 'candidate' with the exact role slug } else { // Default role if no specific selection is made $user = new WP_User($user_id); $user->set_role('subscriber'); // Or any other default role } } } add_action('bp_core_signup_user', 'assign_role_based_on_profile_field', 10, 1);
Make sure the roles employer and candidate are defined in your site.
I want to use the WPJM user roles as standard: employer and candidate. Not sure if the slug meant here is just ’employer’ and ‘candidate’ to be correct?
Best regards,
Flamuren
- You must be logged in to reply to this topic.