Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Different profile types and different user roles

  • Avatar of noizeburger
    noizeburger
    Participant

    @noizeburger

    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 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 4 replies - 1 through 4 (of 4 total)
  • Avatar of Hope
    Hope
    Participant

    @amalsh

    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

    Avatar of Hope
    Hope
    Participant

    @amalsh

    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

    Avatar of Hope
    Hope
    Participant

    @amalsh

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

    Avatar of jonaro
    jonaro
    Participant

    @jonaro

    Thank You very much for this tutorial! :)

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

You must be logged in to reply to this topic.