Skip to:
Content
Pages
Categories
Search
Top
Bottom

Setting User Forum Role By Code


  • yehiasalam
    Participant

    @yehiasalam

    Hello,

    I have a wordpress installation with both bbpress and BuddyPress installed. I’m trying to create a user from code and automatically setting the forum role to participant. I tried using the bp_core_signup_user but it has not effect. This is the part of the code where im creating the user:

    						// create the user from the bp_core method, to make sure it passes the activation mode first
    						$user_id = bp_core_signup_user(
    							$post->username,
    							$post->password,
    							$post->email,
    							[]
    						);
    						bbp_set_user_role( $user_id, "bbp_participant" );

    Attached is the user profile after creation from code:

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

  • Robin W
    Moderator

    @robin-w

    I’d suggest that the issue is that the bp_core_signup_user is not returning a valid $user_id

    you could prove that by writing a console output to see what is being supplied, eg

    // create the user from the bp_core method, to make sure it passes the activation mode first
    						$user_id = bp_core_signup_user(
    							$post->username,
    							$post->password,
    							$post->email,
    							[]
    						);
    echo "<script>console.log('" . $user_id . "');</script>" ;
    
    						bbp_set_user_role( $user_id, "bbp_participant" );

    and then looking in the console in your browser tools

    maybe better to ask the question in ten buddypress support as to why the correct user_id is not being returned


    yehiasalam
    Participant

    @yehiasalam

    I double checked, and its returning the right user id, but im still getting in the Forum Role of the user the — No role for theses forums —


    yehiasalam
    Participant

    @yehiasalam

    it might be worth noting that bp_core_signup_user creates the user in a Pending state first, not sure if this might make a difference.


    Robin W
    Moderator

    @robin-w

    I’ve just done some testing

    this code :

    function test_set_user () {
    $user_id = 1223 ;
    bbp_set_user_role( $user_id, 'bbp_participant' );
    }
    
    add_action ('init' , 'test_set_user' ) ;

    changes user 1223 on site load, so the code is fine.

    I think it is a timing issue – ie when you are doing this. It may well be that even if this fires, a ‘create user’ firing after will overwrite this – eg this may set for pending, but then be overwritten by the final user creation.

    Of course if it is always going to be participant, then bbpress allocates the user to the default role (as set in settings>forums>default role) if no bbpress role is set on first login, so you could just set up the user, and bbpress will take care of giving them a role when they first log in!


    yehiasalam
    Participant

    @yehiasalam

    actually i can conform that, once the user logged in from the web the first time, the participant role was assigned. Still not surw why bbp_set_user_role( $user_id, 'bbp_participant' ); didnt work out, maybe the timing issue as you mentioned. Thanks a lot for taking the time to test out the code and helping me out.


    Robin W
    Moderator

    @robin-w

    no problem, glad you are fixed !

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar