how to get all capabilities regardless of user roles? so far I am only seeing tutorial of how to get capabilities per user. I want to list all available capabilities in one page.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
First, create [all-capabilities] shortcode. Put this code in your theme’s functions.php:
function wpse_all_capabilities() {
$out = '<style>
.flex-columns {
column-count: 4;
column-gap: 3em;
column-rule: 1px solid #000;
}
</style>';
$out .= '<p class="flex-columns">';
$users = get_users();
foreach ( $users as $user ) {
if ( $user->caps['administrator'] ) {
$allcaps = array_keys( $user->allcaps );
foreach ( $allcaps as $cap ) {
$out .= $cap . '<br>';
}
$out .= '</p>';
return $out;
}
}
}
add_shortcode( 'all-capabilities', 'wpse_all_capabilities' );
Update: it is possible that Administrator has some capabilities removed, so we need to scan all users. Modified code follows:
function wpse_all_capabilities() {
$allcaps = array();
$out = '<style>
.flex-columns {
column-count: 3;
column-gap: 3em;
column-rule: 1px solid #000;
}
h2 {
text-align: center;
column-span: all;
}
</style>';
$out .= '<div class="flex-columns">';
$out .= "<h2>All Possible Users' Capabilities<hr></h2><p>";
$users = get_users();
foreach ( $users as $user ) {
$caps = array_keys( $user->allcaps );
foreach ( $caps as $cap ) {
if ( !in_array( $cap, $allcaps, true ) ) {
$num = array_push( $allcaps, $cap );
}
}
}
foreach ( $allcaps as $capability ) {
$out .= $capability . '<br>';
}
$out .= '</p></div>';
return $out;
}
add_shortcode( 'all-capabilities', 'wpse_all_capabilities' );
Now, you can use [all-capabilities] shortcode on your page.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0