bp_current_user_can

Check whether the current user has a given capability.

Description

bp_current_user_can( (string) $capability, (array) $args = array() ); 

Parameters (2)

0. $capability (string)
Capability or role name.
1. $args — Optional. (array) => array()
Array of extra arguments applicable to the capability check.

Options

  • site_id (int) => to the BP root blog

    Blog ID.

  • blog_id (int) => 0

    Deprecated. Use $site_id instead.

array(

    /**
     * Optional. Blog ID.
     *
     * @type int
     * @default to the BP root blog
     * @optional
     */
    'site_id' => to the BP root blog,

    /**
     * Deprecated. Use $site_id instead.
     *
     * @type int
     */
    'blog_id' => 0
);        


Usage

  1. if ( !function_exists( 'bp_current_user_can' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-caps.php'; 
  3.  
  4. // Capability or role name. 
  5. $capability = ''; 
  6.  
  7. // Array of extra arguments applicable to the capability check. 
  8. $args = array( 
  9. 'site_id' => to the BP root blog, 
  10. 'blog_id' => 0 
  11. ); 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = bp_current_user_can($capability, $args); 
  15.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-caps.php  
  1. function bp_current_user_can( $capability, $args = array() ) { 
  2. // Backward compatibility for older $blog_id parameter. 
  3. if ( is_int( $args ) ) { 
  4. $site_id = $args; 
  5. $args = array(); 
  6. $args['site_id'] = $site_id; 
  7.  
  8. // New format for second parameter. 
  9. } elseif ( is_array( $args ) && isset( $args['blog_id'] ) ) { 
  10. // Get the blog ID if set, but don't pass along to `current_user_can_for_blog()`. 
  11. $args['site_id'] = (int) $args['blog_id']; 
  12. unset( $args['blog_id'] ); 
  13.  
  14. // Cast $args as an array. 
  15. $args = (array) $args; 
  16.  
  17. // Use root blog if no ID passed. 
  18. if ( empty( $args['site_id'] ) ) { 
  19. $args['site_id'] = bp_get_root_blog_id(); 
  20.  
  21. /** This filter is documented in /bp-core/bp-core-template.php */ 
  22. $current_user_id = apply_filters( 'bp_loggedin_user_id', get_current_user_id() ); 
  23.  
  24. // Call bp_user_can(). 
  25. $retval = bp_user_can( $current_user_id, $capability, $args ); 
  26.  
  27. /** 
  28. * Filters whether or not the current user has a given capability. 
  29. * @since 1.6.0 
  30. * @since 2.4.0 Pass `$args` variable. 
  31. * @since 2.7.0 Change format of $args variable array. 
  32. * @param bool $retval Whether or not the current user has the capability. 
  33. * @param string $capability The capability being checked for. 
  34. * @param int $blog_id Blog ID. Defaults to the BP root blog. 
  35. * @param array $args Array of extra arguments as originally passed. 
  36. */ 
  37. return (bool) apply_filters( 'bp_current_user_can', $retval, $capability, $args['site_id'], $args );