skip to Main Content

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


  1. Chosen as BEST ANSWER

    I took help from chatgpt and it acctually fixed it. The code works and is like this now:

       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('Roll', $user_id); // Replace 'Role' with the exact name of your profile field
    
                $user = new WP_User($user_id);
    
                // Assign the user role based on the profile field value
                if ($role_value == 'Arbetsgivare') {
                    // Assign 'employer' role
                    $user->set_role('employer'); // Replace 'employer' with the exact role slug
                } elseif ($role_value == 'Arbetssökande') {
                    // Assign 'candidate' role
                    $user->set_role('candidate'); // Replace 'candidate' with the exact role slug
                } else {
                    // Default role if no specific selection is made
                    $user->set_role('subscriber'); // Or any other default role
                }
            }
        }
        add_action('bp_core_activated_user', 'assign_role_based_on_profile_field', 10, 1);
    

  2. Can you please confirm if the given roles exist ? if not you can create roles using below code

    // Register custom roles if they don't already exist
    function register_custom_wpjm_roles() {
        if ( ! get_role( 'employer' ) ) {
            add_role( 'employer', __( 'Employer' ), array( 'read' => true ) );
        }
        if ( ! get_role( 'candidate' ) ) {
            add_role( 'candidate', __( 'Candidate' ), array( 'read' => true ) );
        }
    }
    add_action( 'init', 'register_custom_wpjm_roles' );
    

    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.

    add_action( 'bp_core_activated_user', 'assign_role_based_on_profile_field', 10, 1 );
    

    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.

    function assign_role_based_on_profile_field($user_id) {
        // Ensure BuddyPress and xProfile are active before running
        if (function_exists('xprofile_get_field_data') && $user_id) {
            // Get the custom 'Role' field value
            $role_value = xprofile_get_field_data('Role', $user_id); // Ensure 'Role' matches the exact field name in BuddyPress
            
            if ( $role_value ) {
                $user = new WP_User($user_id);
                
                // Assign roles based on the profile field value
                if ($role_value === 'Employer') {
                    $user->set_role('employer'); // Assumes 'employer' role exists
                } elseif ($role_value === 'Candidate') {
                    $user->set_role('candidate'); // Assumes 'candidate' role exists
                } else {
                    $user->set_role('subscriber'); // Fallback role
                }
            } else {
                error_log("Role value is empty or invalid for user ID: $user_id");
            }
        } else {
            error_log("xProfile not available or invalid user ID: $user_id");
        }
    }
    add_action( 'bp_core_activated_user', 'assign_role_based_on_profile_field', 10, 1 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search