Skip to:
Content
Pages
Categories
Search
Top
Bottom

Passing user role to custom xprofile field


  • noizeburger
    Participant

    @noizeburger

    Hi there,

    I’m searching for a way to pass over a wordpress user role (the user selects its role during registration and should not be able to change this afterwards) to a hidden xprofile field. The role is selected via “WP Roles at Registration” plugin. To make the user role part of buddypress search I would like to pass it over to a custom xprofile field. I need the different user roles to make different profile types available.

    Could this be done? I’m not a programmer – is there a function or something lke that?
    Any help appreciated.
    thanx

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

  • noizeburger
    Participant

    @noizeburger

    No ideas? Every hint would help.


    Henry
    Member

    @henrywright-1

    Are you trying to limit member search results to members of a specific role? e.g. include ‘subscriber’ but exclude ‘author’, ‘editor’ and so on?


    noizeburger
    Participant

    @noizeburger

    @henrywright-1
    this would be one of the possibilities. most important thing is to show the roles in different places in my theme – and make them searchable.
    Generally I have to user roles – band, fan. Beside the “normal” members directory I would like to show them in two separate lists. Whenever users are shown in a list, I want to show their role – similar to the author role, where you can link all the posts of an author I wanna show eg. all bands.
    You understand, what I mean?
    First approaches can be seen here: band profile – if you click on “BAND” you’ll be brought to the band directory.


    Henry
    Member

    @henrywright-1

    @noizeburger I think I get what you mean. To show all members of type ‘fan’ in a “fan directory” you would do this

    <?php
    
    $fans = get_users( array( 'role' => 'fan' ,'fields'=>'ID') );
    $fans = implode( ',', $fans );
    
    if ( bp_has_members( '&include=' . $fans ) ) :
        while ( bp_members() ) : bp_the_member();
            // you can output whatever you like here such as member name, avatar, role and so on
            // we will output just member name for now
            echo bp_member_name();
        endwhile;
    
    else:
        echo 'Sorry, no fans';
    endif;
    ?>

    noizeburger
    Participant

    @noizeburger

    Thanks @henrywright-1
    as you probably noticed, I use page-templates (for which I copied members-loop.php and modified it) for the directories “band” and “fan”. So, the code you wrote should also go there or can I use it everywhere I want?


    Henry
    Member

    @henrywright-1

    @noizeburger yep, it is for use in your templates. For example, your page template could look like this:

    <?php get_header(); ?>
    
    // put the code here
    
    <?php get_footer(); ?>

    noizeburger
    Participant

    @noizeburger

    Thank you @henrywright-1,

    this would be a nice alternative to my own approach, but what about my original idea? Remember you asked the same question in another thread few weeks ago.

    If there would be a way to pass over the different user-roles to xprofile-fields there would be no need to create templates. You could output the field in the member-loop and make it searchable. This would be the simplest way. What do you think about it?

    As an example: a user registers on my site as “band” (which is a wordpress user role). The selected field would be inserted into a (maybe) hidden xprofile-field also called “profiletype”. This field could be echoed everywhere inside buddypress (clickable, searchable). I know this could be done with only xprofile-fields too, but without the possiblity to use bp-xprofile-acl an the advantage of different user capabilities. Am I clear?


    Henry
    Member

    @henrywright-1

    Remember you asked the same question in another thread few weeks ago

    Can you dig out the link and paste it here? I’m trying to understand your exact requirements – and maybe that will help. If I asked the same question then it is possible that I found a solution to it already


    noizeburger
    Participant

    @noizeburger

    It was another post I wrote: Link


    noizeburger
    Participant

    @noizeburger

    btw: with “selected field” I meant selected user_role (in my previous reply above)


    Henry
    Member

    @henrywright-1

    Ah I see! Can you try setting up a new profile field called ‘Role’ – make it of type option list and have ‘fan’ and ‘band’ as the options available.

    This field will then appear on the registration form. As a user signs up they’ll choose if they are a fan or a band. Then that info will be saved as an xprofile field and will be searchable. To my knowledge there is no custom code needed.


    noizeburger
    Participant

    @noizeburger

    Hi @henrywright-1

    if it was that easy I’ve already done it that way. As I said before: I need different user_roles, as bands for example will be able to embed music into their profile, but fans will not, etc.
    If I just make a xprofile field I loose those possibility. I need a custom function.
    Do you understand, what I mean?


    Henry
    Member

    @henrywright-1

    Essentially it sounds like you need two roles. Band and fan.

    Then, once a ‘band’ user has logged in after they’ve registered you want them to be able to embed music into their profile. Users of role ‘fan’ won’t be able to do this.

    On those requirements, why isn’t it as easy as the process outlined in my post?

    Once you have separated the fans from the bands at the point of registration as outlined in my post, you can treat them as discrete entities in BuddyPress. Throughout your theme files you just do some simple logic

    if ( is a band ) {
       // this is a band so let the band play
    } else {
       // this is a fan so do something else
    }

    Hope this info helps.


    noizeburger
    Participant

    @noizeburger

    Hi @henrywright-1 again,

    your logic is right, but I wanted to avoid editing templates. That’s the reason why I use Buddypress xprofiles acl plugin. This makes it possible to choose which role can see and use the different profile tabs. For the music embed I use BP Profile Widgets, the needed input fields are only visible to members with the userrole “band”, all other members can only see the output of the widget – music.
    Your last post makes me think about other things that could be done beside those plugins I use, but that goes too far at the moment. As I tried to explain before, the really important thing about passing over the roles to xprofile-fields would be the possibility to show the roles for each member in a searchable and clickable way that always leads the user to the right member-directory.
    Maybe you have more ideas, however, thanks for your ideas and help.


    Henry
    Member

    @henrywright-1

    I wanted to avoid editing templates

    I suppose you could achieve what you’re trying to do by using a bunch of action hooks although the advantage of theme template files is they are there to be edited.

    Hopefully you’ll be able to get your plugin approach to work! Perhaps it might be worth throwing BP Profile Search into the mix. It offers some improvements over the standard BP member search.


    noizeburger
    Participant

    @noizeburger

    Hi again, @henrywright-1

    just wanted to let you know, that I’ve requested some help from the developer of BP Custom xprofile Fields – donmik

    I asked him, if it is possible, to use a custom xprofile field (selectbox) to choose the user role and pass it over to wordpress. He gave me this function or – you can say – hint:

    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);

    I put this code in my bp-custom.php, but it does not work. I think there’s something missing. Maybe you have an idea, or @modemlooper?


    noizeburger
    Participant

    @noizeburger

    OK, now this topic can be marked as resolved as I finally found a the working solution.

    I made a tutorial in this thread: different profile types, different user roles


    Henry
    Member

    @henrywright-1

    Hi @noizeburger, glad you finally got this working. I’m sure your tutorial will be helpful to many people too. I especially like the last snippet which shows you how to disable the editing of certain fields on the edit profile screen!


    noizeburger
    Participant

    @noizeburger

    Thank you @henrywright-1

Viewing 19 replies - 1 through 19 (of 19 total)
  • The topic ‘Passing user role to custom xprofile field’ is closed to new replies.
Skip to toolbar