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?
I have aksed both Gemini and chatgpt and they seem to confirm that this is correct. But it dont work – so something is not correct and I miss something.
Best regards,
Flamuren
2
Answers
I took help from chatgpt and it acctually fixed it. The code works and is like this now:
Can you please confirm if the given roles exist ? if not you can create roles using below code
The BuddyPress action hook bp_core_signup_user fires when a user signs up but before their account is fully activated. For your case, it’s often more reliable to use the bp_core_activated_user hook, which triggers after account activation and works better in BuddyPress registrations.
Below is the full code but before that please ensure the profile field is exactly named "Role" in BuddyPress (case-sensitive). You can double-check this by navigating to Users > Profile Fields in the WordPress admin. If it’s named differently, update the string in the xprofile_get_field_data function.