BuddyDev

Search

[Resolved] Add a User to a perticular group when User account is activated

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #3287

    Hi,
    Thanks for helping me in last topics. Almost questions are answered and solved quickly and smoothly! Really fantastic!

    I just saw the code snippet in the Support form which is below:
    http://i.imgur.com/ni1FZVl.png?1

    I tried using this snippet in the bp-custom.php, but it doesn’t work.

    So, I just wonder that could you check it again for me?

    Thank you very much.

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #3288

    Hi Dandy,
    Thank you for asking and your support. I appreciate it.

    Most probably you are using a plugin/code that hooks to the activation code at higher priority and exits the process. Can you please try the following code

    
    
    function buddydev_add_user_to_group( $user_id ) {
    
    	$group_id = 1;//which group to add
    	groups_join_group( $group_id, $user_id );
    }
    add_action( 'bp_core_activated_user', 'buddydev_add_user_to_group', 0 );
    
    

    The only thing different here is higher priority on the action. This should work. Can you please check and let me know if it works or not?

    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #3295

    Hi, Brejash
    Thanks for your help.

    I tried the code snippet above but it seems like still can’t work. By the way, I create the hidden Group and I use Administrator account to create new users from back end directly. Will it affect the function of this code snippet?

    Thank you.

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #3303

    Hi Dandy,
    Thank you for the details.
    Yes, That is the problem. The above hook is not getting called since you are using WordPress registration not BuddyPress.

    Please try this hook instead of the activate one

    
    user_register
    

    That should make it work.

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #3306

    Hi, Brajesh
    Thanks for your reply.

    I tested and finally it works perfectly! Thank you so much!

    By the way, I just wonder that is there the same way to add friend with a particular user when a new user is activated? Thank.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #3308

    Hi Dandy,

    Thank You for the acknowledgement. Yes in same way we can add friend to particular user.

    
    function buddydev_add_as_friend_to_user( $user_id ) {
    
        $friend_userid = 2;  //replace by your user id
        friends_add_friend( $user_id, $friend_userid, true );
        
    }
    add_action( 'user_register', 'buddydev_add_as_friend_to_user', 0 );
    

    Thank You
    Ravi

    • This reply was modified 8 years, 7 months ago by Ravi.
  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #3314

    Hi Ravi,
    Thanks so much for your reply and work.

    I already tested this code snippet and it works perfectly! Thank you very much!

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #3315

    Hi, Ravi
    I’m sorry that I have another question about above although I already marked it as resolved.
    I just wonder that is it possible that I add friendships to several users at the same time when new users is activated?
    Thank you very much!

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #3320

    Hi Dandy,
    Ravi will be back a little late today, so I am posting here.

    All you need to do is use an array of friend ids(which you want to add) and loop to do it.

    Here is updated code example.

    
    function buddydev_add_as_friend_to_user( $user_id ) {
    
        $friend_userids = array( 2,3,4,5 );  //use the ids of user who should be automatically added as friend
        
        foreach( $friend_userids as $friend_userid ) {
            friends_add_friend( $user_id, $friend_userid, true );
        }
    
    }
    add_action( 'user_register', 'buddydev_add_as_friend_to_user', 0 );
    
    

    Please do let me know if that works or not?

    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 146
    Dandy Jefferson on #3324

    Hi Brajesh,
    Thanks a million for your reply and work.

    Yeah, it works perfectly!

    Thank you very much.

You must be logged in to reply to this topic.

This topic is: resolved