$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Get current user capabilities|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Get current user capabilities

matteradmin9PV0评论

I am trying to get user capabilities.

$user = wp_get_current_user();

$user_roles = $user->roles;

$allowed_capability = get_role( $user_roles[0] )->capabilities;


var_dump($allowed_capability);

array(10) {
 ["upload_files"]=>
 bool(true)
 ["edit_posts"]=>
 bool(true)
 ["edit_published_posts"]=>
 bool(true)
 ["publish_posts"]=>
  bool(true)
  ["read"]=>
  bool(true)
 ["level_2"]=>
 bool(true)
 ["level_1"]=>
 bool(true)
 ["level_0"]=>
 bool(true)
 ["delete_posts"]=>
 bool(true)
 ["delete_published_posts"]=>
 bool(true)
}

when I try to get first capability:

$user_cap = $allowed_capability[0];

I get:

Notice: Undefined offset: 0 

How do I get "upload_files" for example?

I am trying to get user capabilities.

$user = wp_get_current_user();

$user_roles = $user->roles;

$allowed_capability = get_role( $user_roles[0] )->capabilities;


var_dump($allowed_capability);

array(10) {
 ["upload_files"]=>
 bool(true)
 ["edit_posts"]=>
 bool(true)
 ["edit_published_posts"]=>
 bool(true)
 ["publish_posts"]=>
  bool(true)
  ["read"]=>
  bool(true)
 ["level_2"]=>
 bool(true)
 ["level_1"]=>
 bool(true)
 ["level_0"]=>
 bool(true)
 ["delete_posts"]=>
 bool(true)
 ["delete_published_posts"]=>
 bool(true)
}

when I try to get first capability:

$user_cap = $allowed_capability[0];

I get:

Notice: Undefined offset: 0 

How do I get "upload_files" for example?

Share Improve this question asked May 13 at 16:27 ToniqToniq 4476 silver badges15 bronze badges 2
  • $first = reset( $allowed_capability ); However, keep in mind that an associative array is essentially a dictionary that has no "order". So you might get a different value for the same user at a different time. We could help you better, if you'd explain you use case. – fuxia Commented May 13 at 16:33
  • 2 are you trying to list capabilities or test for a specific one? The approach of testing via [] is very unusual and ignores meta capabilities and filters. Have you looked at the current_user_can function? – Tom J Nowell Commented May 13 at 16:51
Add a comment  | 

1 Answer 1

Reset to default 0

The array you are trying to access is associative so you can't get the data like that...

You access it via the name like so:

$user_cap = $allowed_capability['upload_files'];

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far