bp_current_user_can( string $capability, array|int $args = array() )

Check whether the current user has a given capability.

Description

Parameters

$capability

(string) (Required) Capability or role name.

$args

(array|int) (Optional) Array of extra arguments applicable to the capability check.

  • 'site_id'
    (int) Optional. Blog ID. Defaults to the BP root blog.
  • 'blog_id'
    (int) Deprecated. Use $site_id instead.
  • 'a,...'
    (mixed) Optional. Extra arguments applicable to the capability check.

Default value: array()

Return

(bool) True if the user has the cap for the given parameters.

Source

File: bp-core/bp-core-caps.php

function bp_current_user_can( $capability, $args = array() ) {
	// Backward compatibility for older $blog_id parameter.
	if ( is_int( $args ) ) {
		$site_id = $args;
		$args = array();
		$args['site_id'] = $site_id;

	// New format for second parameter.
	} elseif ( is_array( $args ) && isset( $args['blog_id'] ) ) {
		// Get the blog ID if set, but don't pass along to `current_user_can_for_blog()`.
		$args['site_id'] = (int) $args['blog_id'];
		unset( $args['blog_id'] );
	}

	// Cast $args as an array.
	$args = (array) $args;

	// Use root blog if no ID passed.
	if ( empty( $args['site_id'] ) ) {
		$args['site_id'] = bp_get_root_blog_id();
	}

	/** This filter is documented in /bp-core/bp-core-template.php */
	$current_user_id = apply_filters( 'bp_loggedin_user_id', get_current_user_id() );

	// Call bp_user_can().
	$retval = bp_user_can( $current_user_id, $capability, $args );

	/**
	 * Filters whether or not the current user has a given capability.
	 *
	 * @since BuddyPress 1.6.0
	 * @since BuddyPress 2.4.0 Pass `$args` variable.
	 * @since BuddyPress 2.7.0 Change format of $args variable array.
	 *
	 * @param bool   $retval     Whether or not the current user has the capability.
	 * @param string $capability The capability being checked for.
	 * @param int    $blog_id    Blog ID. Defaults to the BP root blog.
	 * @param array  $args       Array of extra arguments as originally passed.
	 */
	return (bool) apply_filters( 'bp_current_user_can', $retval, $capability, $args['site_id'], $args );
}

Changelog

Changelog
Version Description
BuddyPress 2.7.0 Deprecated $args['blog_id'] in favor of $args['site_id']. BuddyPress 2.7.0 Deprecated $args['blog_id'] in favor of $args['site_id'].
BuddyPress 1.6.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.