• Resolved MathiasKrostewitz

    (@mathiaskrostewitz)


    Hi, first of all thanks for the fgreat plugin.
    We added a field in our registration form called “Role” and manuaaly defined 5 Roles.

    Now i would like to hide profile tabs depend on the role. Have you got any idea how i can achieve that? I tried XProfile ACL Plugin but that only gives me the option to show and hide tabs accoding to wordpress role, what is not clever to use since then everyone will define his role as admin:-)

    Regards
    Mathias

    http://wordpress.org/plugins/buddypress-xprofile-custom-fields-type/

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Miguel López

    (@atallos)

    You can force the wordpress admin role using a hook. Buddypress has various hook in signup, I’m pretty sure you can use one of this.

    If you define 5 roles like this: “role1”, “role2”, “role3”, “role4” and “role5”. You can map each role with a wordpress role like:
    “role1” => “Administrator”
    “role2” => “Subscriber”
    “role3” => “Editor”
    “role4” => “Author”
    “role5” => “Contributor”.

    Then you can use the Xprofile ACL Plugin.

    There is another way also. You can modify your templates and check what role is the user and what group you are going to display.

    donmik, I really love your plugin.
    I also want to play with user role and Xprofile ACL. At the moment I use an older plugin – WP Roles at Registration – to make my users choose their prefered role.
    In your previous answer above you talked about mapping user roles with a custom xprofile field. Can you give some more instructions? I would really like to get an idea on how to “connect” the chosen user role with a hidden custom profile field. That would make it much more easier to work with this field.
    Any ideas?

    Plugin Author Miguel López

    (@atallos)

    Hi,

    Sorry for the delay, I have too much work lately.

    What I said before is you can create a field like a drop down select box and define the roles as options. Then using a registration hook like (here you have more info) you can map the value of this field with the role you want. In this function, you need to check the value selected by user and assign the role you want.

    Then, to hide the field to other users, you can use my plugin and the visibility setting “Nobody”. This will hide the field for all users, only the current user can see it.

    Hi donmik,

    I understand the logic behind your solution, but I’m not a programmer or coder. Maybe I should ask again in the buddypress forums or wait, until you have more time.
    Thanks

    Plugin Author Miguel López

    (@atallos)

    Hi,

    Well, sorry for the delay but I didn’t find the time to do this thing. Today I can try some things and I think it’s working.

    You need to add this filter to your functions.php:

    function custom_bp_core_signup_user($user_id) {
        $user_role = strtolower(xprofile_get_field_data('NAME OF THE XPROFILE FIELD', $user_id));
        switch($user_role) {
            case "role 1":
                $new_role = 'Contributor';
                break;
            case "role 2":
                $new_role = 'Author';
                break;
            default:
            case "role 3":
                $new_role = 'Suscriber';
                break;
        }
        wp_update_user(array(
            'ID' => $user_id,
            'role' => $new_role
        ));
    }
    add_action( 'bp_core_signup_user', 'custom_bp_core_signup_user', 10, 1);

    First I have created an xprofile field (dropdown select box) with the name “NAME OF THE XPROFILE FIELD”. You can give the name you want but you need to write it in the place of this string. The options of the field are the name of the roles: “role 1”, “role 2” and “role 3”. I make this field required and the visibility settings admins only. When a user sign up, he choose the role he want. When he sends the sign up form, this function is executed.

    The function see what $user_role the user has selected searching it by the name of the field and $user_id. Then it changes the role of the user. If the user selects “role 1” we change his role as a contributor. If he chooses “role 2”, he will be an author. If he chooses “role 3”, he will be a suscriber. Suscriber is also the default role.

    Now, you need to decide what you want to do when a user edit his profile. If you don’t want a user be able to change his role, you need to hide this field. I believe you can use this hook “xprofile_screen_edit_profile”. You need to do the same thing, check what value has the field $user_role and then change the role consequently.

    Hi donmik,

    first of all – thank you for the reply.
    I’ve tried to implement this function in two ways: in my functions.php and in my bp-custom.php
    It does not seem to work as there’s no role passed over to wordpress.

    Plugin Author Miguel López

    (@atallos)

    Have you created the field in Profile fields?

    Hi,
    Yes, I followed your instructions.

    Plugin Author Miguel López

    (@atallos)

    I’m sorry but I can’t help you without seeing anything.

    I’ve created the field as a dropdown box, the name of the field was “User Role”. I’ve created 3 options: “Role 1”, “Role 2” and “Role 3”.

    Then in the filter I wrote above, I’ve replaced “NAME OF THE XPROFILE FIELD” with “User Role” and it works as expected… So I don’t know you are missing something here.

    Try to write this line under $user_role = strtolower(xprofile_get_field_data(‘NAME OF THE XPROFILE FIELD’, $user_id));

    $user_role = strtolower(xprofile_get_field_data('NAME OF THE XPROFILE FIELD', $user_id));
    var_dump($user_role); die();

    It will display the value of $user_role, it must be role 1 or role 2 or role 3, what you selected.

    Hi donmik,

    I’ve tried your hint, but it still doesn’t work as expected: what has changed is the fact, that – with your addition above – the chosen role is displayed, but the new users are all added to the default wordpress role.
    On my site, there I don’t use wordpress default roles but 2 custom roles called “Band” and “Fan”. During the process of registration they will be shown, but after login the role is always “none”.

    If you want, I can give you administrator-access.

    HI again,

    good news! no I’ve got it: I left away the default part (role 3) and I wrote the roles in small letters:

    function custom_bp_core_signup_user($user_id) {
        $user_role = strtolower(xprofile_get_field_data('Profilauswahl', $user_id));
        switch($user_role) {
            case "role 1":
                $new_role = 'band';
                break;
            case "role 2":
                $new_role = 'fan';
                break;
        }
        wp_update_user(array(
            'ID' => $user_id,
            'role' => $new_role
        ));
    }

    So, in my case now this seems to work.
    Now, I have to find a way to hide the part, where the users can edit their role as I don’t want them to change it after registration.

    Maybe one more hint, donmik?
    Thanks a lot!

    Ok, so this thread is really resolved: the solution for hide the fields from the edit-screen can be found here: BuddyDev.com

    I think, I put this code snippets together on buddypress.org
    Maybe they will save hours of search for others.

    Really thank you, donmik!

    Plugin Author Miguel López

    (@atallos)

    Great! I’m glad to read you finally solve it. You’re welcome!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Show Profile Tabs depend on Field Value’ is closed to new replies.