bp_group_is_user_banned( BP_Groups_Group|bool $group = false, int $user_id )

Check if a user is banned from a group.

Description

If this function is invoked inside the groups template loop, then we check $groups_template->group->is_banned instead of using groups_is_user_banned() and making another SQL query.

In BuddyPress 2.1, to standardize this function, we are defaulting the return value to a boolean. In previous versions, using this function would return either a string of the integer (0 or 1) or null if a result couldn’t be found from the database. If the logged-in user had the ‘bp_moderate’ capability, the return value would be boolean false.

Parameters

$group

(BP_Groups_Group|bool) (Optional) Group to check if user is banned.

Default value: false

$user_id

(int) (Required) The user ID to check.

Return

(bool) True if user is banned. False if user isn't banned.

Source

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

function bp_group_is_user_banned( $group = false, $user_id = 0 ) {
	global $groups_template;

	// Site admins always have access.
	if ( bp_current_user_can( 'bp_moderate' ) ) {
		return false;
	}

	// Check groups loop first
	// @see BP_Groups_Group::get_group_extras().
	if ( ! empty( $groups_template->in_the_loop ) && isset( $groups_template->group->is_banned ) ) {
		$retval = $groups_template->group->is_banned;

	// Not in loop.
	} else {
		// Default to not banned.
		$retval = false;

		if ( empty( $group ) ) {
			$group = $groups_template->group;
		}

		if ( empty( $user_id ) ) {
			$user_id = bp_loggedin_user_id();
		}

		if ( ! empty( $user_id ) && ! empty( $group->id ) ) {
			$retval = groups_is_user_banned( $user_id, $group->id );
		}
	}

	/**
	 * Filters whether current user has been banned from current group in loop.
	 *
	 * @since BuddyPress 1.5.0
	 * @since BuddyPress 2.5.0 Added the `$group` parameter.
	 *
	 * @param bool   $is_invited If user has been from current group.
	 * @param object $group      Group object.
	 */
	return (bool) apply_filters( 'bp_group_is_user_banned', $retval, $group );
}

Changelog

Changelog
Version Description
BuddyPress 1.5.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.