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

Check whether the current user has a given capability.


Parameters 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()


Top ↑

Return Return

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


Source 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 1.6.0
	 * @since 2.4.0 Pass `$args` variable.
	 * @since 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 );
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.7.0 Deprecated $args['blog_id'] in favor of $args['site_id'].
2.4.0 Second argument modified to accept an array, rather than $blog_id.
1.6.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.