How to Add Members to a Group via WP Users Screen

  • Where we are: users page in the admin screen
  • What we want to do: add one or n members to a group
  • What we want to get: an Add to BP Group menu in the Bulk Actions menu box
  • What we need: a group ID, one or more users. Don’t forget to check the members you want to add!
  • Where to find the group ID ? Hoovering the “edit” button on the groups page will show the ID in the browser bottom left corner, something like http://example.com/wp-admin/admin.php?page=bp-groups&gid=9&action=edit (group ID is 9)

Add this action hook into your theme’s functions.php to provide functionality on a theme basis or add the code to your bp-custom.php file to provide the functionality to your site regardless of theme being used.

Action hook Action hook

Caution: This snippet can only be used on servers running PHP 5.3 and above. For previous versions see below please.

function add_users_to_bpgroup() {
if( bp_is_active(‘groups’) ):

if( isset( $_REQUEST[‘action’] ) && isset( $_REQUEST[‘bp_gid’] ) && isset( $_REQUEST[‘allusers’] ) ) {
$group_id = $_REQUEST[‘bp_gid’];
$users = $_REQUEST[‘allusers’];

foreach ( $users as $user_id ) {
groups_join_group( $group_id, $user_id );
}
}
//form submission
add_action( ‘admin_footer’, function() { ?>
<script type="text/javascript" charset="utf-8">
jQuery("select[name=’action’]").append(jQuery(‘<option value="groupadd">Add to BP Group</option>’));
jQuery("#doaction").click(function(e){
if(jQuery("select[name=’action’] :selected").val()=="groupadd") { e.preventDefault();
gid=prompt("Enter a Group ID","1");
jQuery(".wrap form").append(‘<input type="hidden" name="bp_gid" value="’+gid+’" />’).submit();
}
});
</script>
<?php
});

endif;
}
add_action ( ‘load-users.php’, ‘add_users_to_bpgroup’ );

Top ↑

Use this if your server is running PHP < 5.3 Use this if your server is running PHP < 5.3

Caution: WordPress minimum requirement is PHP 5.2.4

function add_users_to_bpgroup() {
if( bp_is_active(‘groups’) ):

if( isset( $_GET[‘action’] ) && isset( $_GET[‘bp_gid’] ) && isset( $_GET[‘users’] ) ) {
$group_id = $_GET[‘bp_gid’];
$users = $_GET[‘users’];

foreach ( $users as $user_id ) {
groups_join_group( $group_id, $user_id );
}
}

endif;
};
add_action ( ‘load-users.php’, ‘add_users_to_bpgroup’ );

function add_users_to_bpgroup_js() { ?>
<script type="text/javascript" charset="utf-8">
jQuery("select[name=’action’]").append(jQuery(‘<option value="groupadd">Add to Group</option>’));
jQuery("#doaction").click(function(e){
if(jQuery("select[name=’action’] :selected").val()=="groupadd") { e.preventDefault();
gid=prompt("Enter a Group ID","1");
jQuery(".wrap form").append(‘<input type="hidden" name="bp_gid" value="’+gid+’" />’).submit();
}
});
</script>
<?php
};
add_action( ‘admin_footer’, ‘add_users_to_bpgroup_js’ );