Codex Home → Developer Resources → User Submitted Guides → How to Add Members to a Group via WP Users Screen
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
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() { ?>
jQuery("select[name='action']").append(jQuery('Add to BP Group'));
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('').submit();
}
});
<?php
});
endif;
}
add_action ( 'load-users.php', 'add_users_to_bpgroup' );
Use this if your server is running PHP
jQuery("select[name='action']").append(jQuery('Add to Group'));
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('').submit();
}
});
<?php
};
add_action( 'admin_footer', 'add_users_to_bpgroup_js' );