Skip to:
Content
Pages
Categories
Search
Top
Bottom

Allowing new users to select WP role


  • 0901091
    Participant

    @0901091-1

    I’m looking for new users to be able to select from the default WP roles available (Subscriber/Editor/Admin/Contributor). Currently, my WP is set up so new users are Subscriber by default, but I want this to be overwritten by a selection on the registration form.

    So far, I have a new xprofile group called ‘User Role’ with the four options, and within by bp-custom.php file I have:

    function register_role($user_id) {
        $user_role = strtolower(xprofile_get_field_data('User Role', $user_id));
        switch($user_role) {
            case "Editor":
                $new_role = 'editor';
                break;
            case "Contributor":
                $new_role = 'contributor';
                break;
    		case "Admin":
                $new_role = 'admin';
                break;
    		case "Subscriber":
                $new_role = 'subscriber';
                break;
        }
        wp_update_user(array(
            'ID' => $user_id,
            'role' => $new_role
        ));
    }
    add_action( 'user_register', 'register_role');
    

    This doesn’t work though, it still creates new user entries with the default role of Subscriber. I have also tried the plugin WordPress Roles at Registration, with also didn’t work. Any ideas?

Viewing 6 replies - 1 through 6 (of 6 total)

  • 0901091
    Participant

    @0901091-1

    It does store the correct value within the xprofile group, but not within WP itself. I need it to override WP default value of Subscriber.


    0901091
    Participant

    @0901091-1

    Solved:

    Removing the email verification has solved the problem. Turns out that the add_action wouldn’t work for accounts that aren’t already activated.


    Rune Kragh Rasmussen
    Participant

    @rune-kragh-rasmussen

    Can you please explain what you did to solve it. I desperately would like to know how to let members choose their user roles on registration.

    I have created a new xprofile group called ‘User Role’, with the following values (‘Editor’, ‘Contributor’, ‘Admin’ and ‘Subscriber’.

    After this I pasted the following code into “bp-custom.php”:

    function register_role($user_id) {
        $user_role = strtolower(xprofile_get_field_data('User Role', $user_id));
        switch($user_role) {
            case "Editor":
                $new_role = 'editor';
                break;
            case "Contributor":
                $new_role = 'contributor';
                break;
    		case "Admin":
                $new_role = 'admin';
                break;
    		case "Subscriber":
                $new_role = 'subscriber';
                break;
        }
        wp_update_user(array(
            'ID' => $user_id,
            'role' => $new_role
        ));
    }
    add_action( 'user_register', 'register_role');

    What more do i need to do to make it work?


    aaronkine
    Participant

    @aaronkin

    I also would like to know how to make this work.


    evitorino
    Participant

    @evitorino

    @0901091-1 Can you please post the steps you made to make this work?

    I am in need to provide user choice of role at registration and would like to know how you made it.

    Thank you


    aaronkine
    Participant

    @aaronkin

    I used a plugin called WordPress User Frontend Pro and their Buddypress addon. I also used this plugin to create new User Roles “Capability Manager Enhanced”

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Allowing new users to select WP role’ is closed to new replies.
Skip to toolbar