groups_invite_user( array|string $args = '' )

Invite a user to a group.

Description

Parameters

$args

(Optional) Array of arguments.

  • 'user_id'
    (int) ID of the user being invited.
  • 'group_id'
    (int) ID of the group to which the user is being invited.
  • 'inviter_id'
    (int) Optional. ID of the inviting user. Default: ID of the logged-in user.
  • 'date_modified'
    (string) Optional. Modified date for the invitation. Default: current date/time.
  • 'content'
    (string) Optional. Message to invitee.
  • 'send_invite'
    (bool) Optional. Whether the invitation should be sent now. Default: false.

Default value: ''

Return

(bool) True on success, false on failure.

Source

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

function groups_invite_user( $args = '' ) {
	$bp = buddypress();

	$args = bp_parse_args( $args, array(
		'user_id'       => false,
		'group_id'      => false,
		'inviter_id'    => bp_loggedin_user_id(),
		'date_modified' => bp_core_current_time(),
		'is_confirmed'  => 0
	), 'groups_invite_user' );
	extract( $args, EXTR_SKIP );

	if ( ! $user_id || ! $group_id || ! $inviter_id ) {
		return false;
	}

	// If the user has already requested membership, accept the request.
	if ( $membership_id = groups_check_for_membership_request( $user_id, $group_id ) ) {
		groups_accept_membership_request( $membership_id, $user_id, $group_id );

	// Otherwise, create a new invitation.
	} elseif ( ! groups_is_user_member( $user_id, $group_id ) && ! groups_check_user_has_invite( $user_id, $group_id, 'all' ) ) {
		$invite                = new BP_Groups_Member;
		$invite->group_id      = $group_id;
		$invite->user_id       = $user_id;
		$invite->date_modified = $date_modified;
		$invite->inviter_id    = $inviter_id;
		$invite->is_confirmed  = $is_confirmed;

		if ( !$invite->save() )
			return false;

		// update user meta with invite message for a group
		if ( ! empty( $bp->groups->invites_message ) ) {
			update_user_meta( $user_id, 'bp_group_invite_message_' . $group_id, $bp->groups->invites_message );
		}

		/**
		 * Fires after the creation of a new group invite.
		 *
		 * @since BuddyPress 1.0.0
		 *
		 * @param array $args Array of parsed arguments for the group invite.
		 */
		do_action( 'groups_invite_user', $args );
	}

	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.