Question

I'm trying to put all possible capabilities into a dropdown list. I'm new to this so go easy on me. What I have at the moment is not working in the slightest bit:

$user = get_user_by('id', '1');
$capslist = $user->allcaps; 
$dropdown = '<select>';
foreach($capslist as $cap){
$dropdown .= '<option value="'.$cap.'">'.$cap.'</option>';
}
$dropdown .= '</select>';
return $dropdown;

EDIT --

I made it a shortcode just for a quick test:

add_shortcode('capsdropdown', 'sc_capsdropdown');
function sc_capsdropdown($attr) {
$user = get_user_by('id', '1');
$capslist = $user->allcaps; 
$dropdown = '<select>';
foreach($capslist as $cap){
$dropdown .= '<option value="'.$cap.'">'.$cap.'</option>';
}
$dropdown .= '</select>';
return $dropdown;
}

and it DOES create a dropdown. Problem is, it's boolean. It's all 1s. Any way to get it to return the actual cap names?

-- EDIT: It's weird because vardump($user->allcaps); returns the actual values, not boolean. I tried using settype but it didn't affect the output.

Was it helpful?

Solution

foreach($capslist as $k=>$cap){
    $dropdown .= '<option value="'.$k.'">'.$k.'</option>';
}

My guess was how the array was structured. Each Array Key was the actual name of the capability, with its value being a simple boolean determining whether or not the given user instance had the permission to perform that given action.

You were searching for the capabilities by name, and since you were only seeing 1s in the output, I figured what you were looking for was in the keys.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top