groups_accept_invite( int $user_id, int $group_id )

Process the acceptance of a group invitation.

Description

Returns true if a user is already a member of the group.

Parameters

$user_id

(Required) ID of the user.

$group_id

(Required) ID of the group.

Return

(bool) True when the user is a member of the group, otherwise false.

Source

File: bp-groups/bp-groups-functions.php

function groups_accept_invite( $user_id, $group_id ) {

	// If the user is already a member (because BP at one point allowed two invitations to
	// slip through), delete all existing invitations/requests and return true.
	if ( groups_is_user_member( $user_id, $group_id ) ) {
		if ( groups_check_user_has_invite( $user_id, $group_id ) ) {
			groups_delete_invite( $user_id, $group_id );
		}

		if ( groups_check_for_membership_request( $user_id, $group_id ) ) {
			groups_delete_membership_request( null, $user_id, $group_id );
		}

		return true;
	}

	$member = new BP_Groups_Member( $user_id, $group_id );

	// Save the inviter ID so that we can pass it to the action below.
	$inviter_id = $member->inviter_id;

	$member->accept_invite();

	if ( !$member->save() ) {
		return false;
	}

	// Remove request to join.
	if ( $member->check_for_membership_request( $user_id, $group_id ) ) {
		$member->delete_request( $user_id, $group_id );
	}

	// Modify group meta.
	groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );

	/**
	 * Fires after a user has accepted a group invite.
	 *
	 * @since BuddyPress 1.0.0
	 * @since BuddyPress 2.8.0 The $inviter_id arg was added.
	 *
	 * @param int $user_id    ID of the user who accepted the group invite.
	 * @param int $group_id   ID of the group being accepted to.
	 * @param int $inviter_id ID of the user who invited this user to the group.
	 */
	do_action( 'groups_accept_invite', $user_id, $group_id, $inviter_id );

	return true;
}

Changelog

Changelog
Version Description
BuddyPress 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.