How to get all capabilities

时间:2021-04-22 作者:murni

如何在不考虑用户角色的情况下获得所有功能?到目前为止,我只看到了如何获得每个用户的功能的教程。我想在一页中列出所有可用的功能。

1 个回复
最合适的回答,由SO网友:Frank P. Walentynowicz 整理而成

首先,创建[all-capabilities] 短代码。将此代码放入主题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: 有可能Administrator 删除了一些功能,因此我们需要扫描所有用户。修改代码如下:

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\' );
现在,您可以使用[all-capabilities] 页面上的快捷代码。

相关推荐

正在尝试获取wp-includes/capabilities.php中非对象的属性

在调试中,我每分钟都会收到以下通知序列。日志:[23-Oct-2012 13:27:33 UTC] PHP Notice: Trying to get property of non-object in mysite/wp-includes/capabilities.php on line 1022 [23-Oct-2012 13:27:33 UTC] PHP Notice: Trying to get property of non-object in mysite/wp-includes/