Can you explain how to assign a buddypress user group

Can you explain how to assign a buddypress user group to a subscription...

  • Milan

    Hi @David Jolley ,

    I hope you are well today and thanks for posting.. :slight_smile:

    Do you want to assign subscriber who are created with membership 2 pro to buddy-press group or something else ? Please give me little bit more info please.

    Please provide me little bit more information so that I can provide you proper solution for this. :slight_smile:

    Thanks for taking time to ask a question. :slight_smile:

    Regards,
    Milan Savaliya
    Support Staff ( WPMUDEV )

  • Milan

    Hi @David Jolley,

    I am sorry for being late here and being not Cristal clear how you can use this code.

    Purpose of above code is fairly simple, this code will automatically assign users who subscribes to membership to buddypress group. For this you only need to modify this code just a little bit to let it know which membership to map with which buddypress group. :slight_smile:

    Let me make it simple. Here I am assuming that you are using single site environment.
    You have two options to add this functionality.

    #1) Add this functionality to theme
    #2) Add this to child theme.
    #2) Create stand alone plugin for this.

    First approach is not future safe as your changes will be removed when theme gets updated. Still if you want to do so you can paste code described in link to functions.php file of your current theme. File path of that file will be /wp-contents/themes/<active_theme_name>/functions.php .

    Second approach is to use child theme. You can learn more about how to create child theme here https://codex.wordpress.org/Child_Themes. In this approach too you will copy code to child themes functions.php file. But in this case your code will not be removed when theme gets updated. :slight_smile:

    Third approach is to create stand alone plugin and paste code to plugin file. This will make your code independent to theme and makes future safe. :slight_smile: To learn more about plugins you can read here. https://codex.wordpress.org/Writing_a_Plugin.

    After deciding which approach is best for you, you only need to just modify code little bit.

    here is sample code give in link.

    <?php
    
    add_action( 'ms_model_relationship_create_ms_relationship_before', 'ms_controller_member_assign_memberships_done_cb', 99, 4 );
    function ms_controller_member_assign_memberships_done_cb( $membership_id, $user_id, $gateway_id, $move_from_id ) {
    
        // Create an array with membership IDs and group IDs like:
        /*$array = array(
            'membership_id' => 'group_id',
            'membership_id' => 'group_id',
            'membership_id' => 'group_id'
        );*/
    
        $config = array(
            '11' => 4,
            '19' => 3,
            '20' => 2
        );
    
        groups_join_group( $config[$membership_id], $user_id );
    }

    As you can see you need to setup config array( $config ). The key of $config array will be your membership id and value of that key will be buddypress group id.

    So if your membership "My Awesome Mebership" id is '3' and your buddypress group "My Awesome Buddypress group" id is '5'. Then if you want that all of the subscribers of "My Awesome Membership" should be assign to "My Awesome BuddyPress Group" You will setup your config array as

    $config = array ( '3' => '5' );

    I hope this helps you..

    Please let me know if there is anything else I can help you with..

    Enjoy Wpmudev..

    Warmest Regards,
    Milan..

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.