BP_Groups_Member::delete_all_for_user( int $user_id )

Delete all group membership information for the specified user.


Description Description

In cases where the user is the sole member of a group, a site administrator is assigned to be the group’s administrator. Unhook groups_remove_data_for_user() to modify this behavior.


Top ↑

Parameters Parameters

$user_id

(int) (Required) ID of the user.


Top ↑

Return Return

(bool)


Source Source

File: bp-groups/classes/class-bp-groups-member.php

	public static function delete_all_for_user( $user_id ) {
		$group_ids = BP_Groups_Member::get_group_ids( $user_id );

		foreach ( $group_ids['groups'] as $group_id ) {
			if ( groups_is_user_admin( $user_id, $group_id ) ) {
				// If the user is a sole group admin, install a site admin as their replacement.
				if ( count( groups_get_group_admins( $group_id ) ) < 2 ) {
					$admin = get_users( array(
						'blog_id' => bp_get_root_blog_id(),
						'fields'  => 'id',
						'number'  => 1,
						'orderby' => 'ID',
						'role'    => 'administrator',
					) );

					if ( ! empty( $admin ) ) {
						groups_join_group( $group_id, $admin[0] );

						$member = new BP_Groups_Member( $admin[0], $group_id );
						$member->promote( 'admin' );
					}
				}
			}

			BP_Groups_Member::delete( $user_id, $group_id );
		}

		return true;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
4.0.0 The method behavior was changed so that single-member groups are not deleted.
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.