Skip to:
Content
Pages
Categories
Search
Top
Bottom

How To bp_set_member_type?


  • ch1n3s3b0y
    Participant

    @ch1n3s3b0y

    Is there going to be a way where we can set a user’s member type from the admin section? Also, can we set a default member type?

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

  • youmin
    Participant

    @youmin

    I am also waiting for the tutorial for this, plus adding user roles and restricting users capabilities by member types and their assigned roles.
    Since buddypress 2.2 is in beta stage most of the devs are playing with this new query so that they can provide error less tutorials and plug ins. So wait till bp 2.2 officially launch. In the top announcement one of the dev has promised for the tutorial .


    Henry Wright
    Moderator

    @henrywright

    Hi @ch1n3s3b0y

    To set a default member type, try adding this to either your theme’s functions.php file or your bp-custom.php file.

    function my_set_default_member_type( $user_id, $user_login, $user_password, $user_email, $usermeta ) {
        // Set the member's type to student
        bp_set_member_type( $user_id, 'student' );
    }
    add_action( 'bp_core_signup_user', 'my_set_default_member_type', 10, 5 );

    youmin
    Participant

    @youmin

    Thanks henry, but how about adding 3 different types of member types Eg : 1) student. 2)staff 3) collage ?
    And how to assign the user roles? I mean students are just subscribers , they don’t need to create a blog & group etc [ some sort of restrictions ] and college user type can have admin rights etc . thank you once again .


    ch1n3s3b0y
    Participant

    @ch1n3s3b0y

    Would we use something like this to set the user role at registration:

    [edited – please use ‘code’ button when sharing code]

    function my_set_default_member_type( $user_id, $user_login, $user_password, $user_email, $usermeta ) {
        // Set the member's type to student
        bp_set_member_type( $user_id, 'student' );
        $user_id->set_role( 'contributor' );
    }
    add_action( 'bp_core_signup_user', 'my_set_default_member_type', 10, 5 );

    Not sure how we would change the user role when the member type is changed in admin.


    youmin
    Participant

    @youmin

    I too doubt this code,we need to set the array in loop,by using if and else statements. It will be great help if some one make it here .


    Henry Wright
    Moderator

    @henrywright

    Hi guys

    My snippet shows you how to set a default type as you asked for this in the second half of your question. It won’t be useful if you want to set a member type from the admin area. That’ll need a different solution.

    Hope this helps.


    youmin
    Participant

    @youmin

    thanks Henry, will you help us to sort it out. take a look at this code.

     bp_register_member_type( 'student', array(
            'labels' => array(
                'name'          => 'Student',
                'singular_name' => 'student',
    	    'role'          => 'subscriber'
            ),
        ) );
    }
    add_action( 'bp_init', 'bbg_register_member_types' );
    

    will this code set a user role as subscriber ?

    and lastly after registering member type by using above code, how show member type ‘name’ in header (or in meta fileld or out put it any where ) ?

    thank you


    r-a-y
    Keymaster

    @r-a-y

    Roles are independent from BP member types.

    As per the codex article, adding a ‘role’ parameter to your bp_register_member_type() call will not do anything:

    Member Types

    If you have concerns about this, I would suggest you raise a ticket if you want to tie roles to member types:
    https://trac.buddypress.org/newticket

    Use the same credentials you use here on buddypress.org.


    youmin
    Participant

    @youmin

    Will you kindly provide me the snippet based on this ticket, as you directed I had raised this ticket and I am happy that their is a solution.

    https://buddypress.trac.wordpress.org/ticket/6154#ticket”

    Thank you once again.


    Henry Wright
    Moderator

    @henrywright

    Hi @youmin

    If you don’t already have your roles set-up then this is how you’d add them:

    add_role(
        'student',
        __( 'Student' ),
        array(
            'read'         => true,  // true allows this capability
            'edit_posts'   => true,
            'delete_posts' => false, // Use false to explicitly deny
        )
    );

    The 3rd parameter is where you’d put your capabilities. In my example above, I’ve created a ‘student’ role which can read and edit posts but can’t delete posts. Feel free to amend these capabilities.

    Ref: https://codex.wordpress.org/Function_Reference/add_role

    Then to pick up on Boone’s suggestion in the Trac ticket, you could do this:

    function youmin_set_role( $user_id, $member_type, $append ) {
        $userdata = array( 'ID' => $user_id, 'role' => $member_type );
        wp_update_user( $userdata );
    }
    add_action( 'bp_set_member_type', 'youmin_set_role', 10, 3 );

    When you use the bp_set_member_type() function like I have done here, this code will assign a role to the user who is signing up. The role that is assigned will be the type you use in bp_set_member_type().

    Hope this helps you.


    youmin
    Participant

    @youmin

    Thank you sir its a great relief now. Now I got it.
    I have already assigned the roles now I’ll modify it by this snippet I.e adding and modifying array.
    Thank you once again


    youmin
    Participant

    @youmin

    In my above comment i mean to say , I have already set member types using bp_set_member_type function (not roles ) .
    So from above snippet I can set roles to these created member types isn’t it ? Or else still these two functions are independent ?


    Henry Wright
    Moderator

    @henrywright

    If you look at my snippet here, you will see that I’ve hooked the my_set_default_member_type() function to bp_core_signup_user. Now, bp_core_signup_user fires each time a new user registers on your website. This means that bp_set_member_type( $user_id, 'student' ) executes each time a new user registers on your site.

    Then if you look here, you’ll see I’ve hooked my youmin_set_role() function to the bp_set_member_type hook (note that the bp_set_member_type hook and the bp_set_member_type() function are different things). This means that when the bp_set_member_type hook fires, the code inside youmin_set_role() will execute.

    So as you can see, you have to use both snippets together, exactly as I’ve written them, in order for things to work.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘How To bp_set_member_type?’ is closed to new replies.
Skip to toolbar