Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to Add Members to a Group via WP Users Screen

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( $_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 );
			}
		}
		//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' );

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' );