Skip to:
Content
Pages
Categories
Search
Top
Bottom

Different profile types and different user roles

  • @noizeburger

    Participant

    This topic has been always important to my community project. Throughout the last 4 years I’ve tried several ways to reach a way working with different user profiles. If you search the forums you’ll see that others also did and still do.

    Maybe my approach/solution will fit some of the requests.
    First of all, I’ve to say THANK YOU to the following people/users:


    @henrywright-1
    for some hints into the right direction
    donmik, publisher of the wonderful Buddypress Xprofile Custom Fields Type plugin
    and Brajesh Singh @sbrajesh for his usefull code snippets.

    So, what I needed and what was the solution?

    I’m working on a platform for musicians and music fans. To give both usertypes different capabilities in their profiles I decided to give them different user roles.
    To achive this, there are several wordpress plugins. Below you will find a list of the plugins I use.
    In earlier versions of Buddypress I’ve used the WP Roles at Registration plugin together with Buddypress xProfiles ACL plugin. So I had the possibility to make the user chose a role at registration and a way to define what buddypress profile groups are shown in the different profile types. Both plugins haven’t been updated in the last two years but both still work. xProfiles ACL needs to be hacked – see this support thread .

    So far so good. But there is no way to include the user role (in my case “band” and “fan”) in buddypress search. My first attempt was to include the user role via some code snippet in my member-loop, but that seemed a bit complicated to me. I wanted a conjunction between the user role and buddypress search. So there had to be a way to select the user role through a xprofile field, ’cause xprofile fields can be searched and also displayed in the member-loop in several ways.

    To stop confusing you, here’s what I’ve done:

    required plugins:

    1. Buddypress xProfiles ACL with the hack mentioned a few lines earlier.
    2. Capability Manager Enhanced to define custom user roles, if you’re not satisfied with the default wordpress roles (subscriber, contributor,…).

    The first step is to create your user roles, modify the ACL plugin and define some profile field groups that can be finally added to the different user roles/types.
    Create a xprofile field (selectbox) with the title “User Role” and name the options “Band Role” and “Fan Role” – in my case. You can call them whatever you want. Place this field in the “base” profile group, otherwise it won’t be shown during registration. Also make the field “required”. In my case the user can’t decide, if the visibility of the field can be changed.

    In a second step you need to create a bp-custom.php in the root of your wordpress plugin folder (if you haven’t done this yet, cause you have other custom functions).
    Put the following snippet in your bp-custom:

    <?php
    function custom_bp_core_signup_user($user_id) {
        $user_role = strtolower(xprofile_get_field_data('User Role', $user_id));
        switch($user_role) {
            case "Band Role":
                $new_role = 'band';
                break;
            case "Fan Role":
                $new_role = 'fan';
                break;
        }
        wp_update_user(array(
            'ID' => $user_id,
            'role' => $new_role
        ));
    }
    add_action( 'bp_core_signup_user', 'custom_bp_core_signup_user', 10, 1);

    As you can see, I’ve included the title of the xprofile field in line 3. It’s important to put in the exact title of your xprofile field. The case “Band Role” and case “Fan Role” is the place to put your select options of the field. Finally $new_role has to be filled with the wordpress or custom user roles – important, otherwise it won’t work: in lowercase letters – in my case “band” and “fan”.

    Save your bp-custom.php and you’re nearly done.
    In my case I want to prevent the users to change their user role after registration (if you want to give them the possibility to do so, more hacking has to be done – but I’m not sure how to). Put another snippet in your bp-custom.php:

    //hide the user role select field in edit-screen to prevent changes after registration
    add_filter("xprofile_group_fields","bpdev_filter_profile_fields_by_usertype",10,2);
    function bpdev_filter_profile_fields_by_usertype($fields,$group_id){
    //only disable these fields on edit page
    if(!bp_is_profile_edit())
    return $fields;
    //please change it with the name of fields you don't want to allow editing
    $field_to_remove=array("User Role");
    $count=count($fields);
    $flds=array();
    for($i=0;$i<$count;$i++){
    if(in_array($fields[$i]->name,$field_to_remove))
    unset($fields[$i]);
    else
    $flds[]=$fields[$i];//doh, I did not remember a way to reset the index, so creating a new array
    }
    return $flds;
    }

    Again, fill in the title of the profile field (User Role), where it’s indicated in the code.

    That’s it. I hope I haven’t been too confusing. Ask if you need help.

Viewing 8 replies - 1 through 8 (of 8 total)
  • @amalsh

    Participant

    Thanks a lot @noizeburger for this snippet, just what I want & it works just fine with me. There’s only one note: The cases have to be in small letters since you’re converting to small letters. I mean for “Band Role” & “Fan Role”.

    I also don’t want for members to be able to change their roles, I did it with a simple java script to show a corresponding message when the member tries to change his role since I don’t want to hide the whole field.

    Thanks again!
    Hope

    @amalsh

    Participant

    A question plz:
    If I want to allow editing the “user Role” field and do the same thing. i.e. saving the actual role upon changing the “User Role” field, what function I have to modify? Do you have any idea?

    Thanks
    Hope

    @amalsh

    Participant

    I found it, it’s “xprofile_updated_profile”… thanks.

    @jonaro

    Participant

    Thank You very much for this tutorial! 🙂

    @lebearuk

    Participant

    @noizeburger Thank you for sharing this – it’s helped me out greatly.

    I might just add to anyone else who’s going to implement this as I’d spent about four hours trying to figure out why it wouldn’t work for me initially.

    add_action( 'bp_core_signup_user', 'custom_bp_core_signup_user', 10, 1);

    The number 10 states at what point the function should be called. This should work in most instances but I am using a theme with additional buddypress functions built in.

    I had to set this to a higher level, meaning it gets executed later on in the process. At least I think that’s right. Anyhow, once I’d changed mine to

    add_action( 'bp_core_signup_user', 'custom_bp_core_signup_user', 13, 1);

    It worked fine!

    It’s worth reading this.

    https://codex.wordpress.org/Function_Reference/add_action

    Thanks again

    @lebearuk

    Participant

    My bad, I’ve just realised that wasn’t the solution…

    I had to add the code in this function

    xprofile_sync_wp_profile

    in

    wp-content/plugins/buddypress/bp-xprofile/bp-xprofile-screens.php

    I’m not sure why it doesn’t work in the bp-custom.php file…

    @kamillamonkey

    Participant

    @noizeburger i have followed all the instructions. I don’t get any errors. My problem is the User Role doesn’t change upon new registration. It wont change it from the DEFAULT ROLE set in WordPress/Settings/General/New User Default Role [Author]. Any Suggestions?

    @jeffacubed

    Participant

    I’m in exactly in the same boat as @kamillamonkey on this one – “It wont change it from the DEFAULT ROLE set in WordPress/Settings/General/New User Default Role [Subscriber].”

    I can go in after a new user registers and ‘manually’ switch the user role, but alas, at least for me, I’m not getting the roles assigned based on the user_roles & cases as outlined above when they register.

    I’m using BuddyPress 2.0.1 & WordPress 3.9.1

    Seems odd that the new user role would not be assigned, as the tutorial/code in @noizeburger ‘s example looks very solid! Any chance this has to do with something new/recent in BuddyPress 2.0.1?

    -Jeff

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Different profile types and different user roles’ is closed to new replies.
Skip to toolbar