groups_send_membership_request( array|string $args = array() )

Create a group membership request.

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.
  • 'content'
    (string) Optional. Message to invitee.
  • 'date_modified'
    (string) Optional. Modified date for the invitation. Default: current date/time.

Default value: array()

Return

(bool) True on success, false on failure.

Source

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

function groups_send_membership_request( $requesting_user_id, $group_id ) {

	// Prevent duplicate requests.
	if ( groups_check_for_membership_request( $requesting_user_id, $group_id ) )
		return false;

	// Check if the user is already a member or is banned.
	if ( groups_is_user_member( $requesting_user_id, $group_id ) || groups_is_user_banned( $requesting_user_id, $group_id ) )
		return false;

	// Check if the user is already invited - if so, simply accept invite.
	if ( groups_check_user_has_invite( $requesting_user_id, $group_id ) ) {
		groups_accept_invite( $requesting_user_id, $group_id );
		return true;
	} else {

		if ( true === bp_member_type_enable_disable() && true === bp_disable_group_type_creation() ) {

			$group_type = bp_groups_get_group_type( $group_id );

			$group_type_id = bp_get_group_type_post_id( $group_type );

			$get_selected_member_type_join = get_post_meta( $group_type_id, '_bp_group_type_enabled_member_type_join', true );

			$get_requesting_user_member_type = bp_get_member_type( $requesting_user_id );

			if ( in_array( $get_requesting_user_member_type, $get_selected_member_type_join ) ) {
				groups_accept_invite( $requesting_user_id, $group_id );
				return true;
			}

		}
	}

	$requesting_user                = new BP_Groups_Member;
	$requesting_user->group_id      = $group_id;
	$requesting_user->user_id       = $requesting_user_id;
	$requesting_user->inviter_id    = 0;
	$requesting_user->is_admin      = 0;
	$requesting_user->user_title    = '';
	$requesting_user->date_modified = bp_core_current_time();
	$requesting_user->is_confirmed  = 0;
	$requesting_user->comments      = isset( $_POST['group-request-membership-comments'] ) ? $_POST['group-request-membership-comments'] : '';

	if ( $requesting_user->save() ) {
		$admins = groups_get_group_admins( $group_id );

		// Saved okay, now send the email notification.
		for ( $i = 0, $count = count( $admins ); $i < $count; ++$i )
			groups_notification_new_membership_request( $requesting_user_id, $admins[$i]->user_id, $group_id, $requesting_user->id );

		/**
		 * Fires after the creation of a new membership request.
		 *
		 * @since BuddyPress 1.0.0
		 *
		 * @param int   $requesting_user_id  ID of the user requesting membership.
		 * @param array $admins              Array of group admins.
		 * @param int   $group_id            ID of the group being requested to.
		 * @param int   $requesting_user->id ID of the membership.
		 */
		do_action( 'groups_membership_requested', $requesting_user_id, $admins, $group_id, $requesting_user->id );

		return true;
	}

	return false;
}

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.