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
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 —
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.
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!
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.
no problem, glad you are fixed !