3

Trying to put a list of all possible caps in a dropdown. I can only get it to return 1s (boolean true). If I use var_dump($user->allcaps); it does produce the actual cap names, but when I plug this into my function, it goes to boolean. Have tried using various settype statements; nothing has worked. Here's the function currently:

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;
}

Resolved. This works:

foreach($capslist as $cap=>$caps){ $dropdown .= '<option value="'.$cap.'">'.$cap.'</option>'; }
  • Resolved. This works: foreach($capslist as $cap=>$caps){ $dropdown .= '<option value="'.$cap.'">'.$cap.'</option>'; } – Works for a Living Oct 5 '13 at 2:07
  • Hi, Thom, please read the etiquette regarding multi-posts and add your solution as an Answer, so this Question doesn't remain on the unanswered list, thanks! – brasofilo Oct 5 '13 at 13:03
  • 1
    I tried to answer it but it told me to come back in six hours. So I had planned to do so. – Works for a Living Oct 5 '13 at 15:23
  • brasofilo, thanks for the link to the etiquette. I apologize for that. Won't happen again. – Works for a Living Oct 5 '13 at 17:19
1

Solved with this Answer at Stack Overflow.

Each Array Key was the actual name of the capability [...] 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.

foreach($capslist as $cap=>$caps){ 
    $dropdown .= '<option value="'.$cap.'">'.$cap.'</option>'; 
} 
  • 2
    Can you explain why that works, so future visitors with a similar problem understand the solution? – s_ha_dum Oct 5 '13 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.