function.php中的Wordpress功能和current_user_can()

时间:2012-11-15 18:59:09

标签: wordpress

我已经在functions.php中添加了一个函数,用于在登录后将用户重定向到posts-new.php并且它可以正常工作。但是,如果登录的用户是贡献者,我只希望发生这种情况。所以我添加了以下内容:

/** Redirect after login */
    function mysite_login_redirect(){
        if ( current_user_can( 'manage_options' ) ) {
           return 'http://mysite.com/wp-admin/index.php';}
        else {
           return 'http://mysite.com/wp-admin/post-new.php';}
    }
add_action( 'login_redirect', 'mysite_login_redirect');

在此状态下,贡献者和管理员都被重定向到post-new.php。为了测试它,我修改了函数,以便重定向没有该功能的用户:

if ( !current_user_can( 'ma ...

当我修改函数时,贡献者和管理员都被重定向到index.php。

因此该功能似乎有效,但这对我来说意味着它没有看到管理员的'manage_options'功能。我尝试了几个具有相同结果的管理员独有功能。很奇怪吧?

我应该说我使用的是用户角色编辑器插件,但是我禁用了它并测试了相同结果的函数。

我也在使用Active Directory集成和管理菜单编辑器。

2 个答案:

答案 0 :(得分:14)

试试这个:

if( current_user_can( 'administrator' ) ){} // only if administrator
if( current_user_can( 'editor' ) ){} // only if editor
if( current_user_can( 'author' ) ){} // only if author
if( current_user_can( 'contributor' ) ){} // only if contributor
if( current_user_can( 'subscriber' ) ){} // only if subscriber

或者:

if( current_user_can( 'level_10' ) ){}
if( current_user_can( 'level_9' ) ){}
if( current_user_can( 'level_8' ) ){}
if( current_user_can( 'level_7' ) ){}
if( current_user_can( 'level_6' ) ){}
if( current_user_can( 'level_5' ) ){}
if( current_user_can( 'level_4' ) ){}
if( current_user_can( 'level_3' ) ){}
if( current_user_can( 'level_2' ) ){}
if( current_user_can( 'level_1' ) ){}
if( current_user_can( 'level_0' ) ){}

答案 1 :(得分:0)

尝试一下:

$exclude_role = 'contributor';
        $roles = get_role( $exclude_role )->capabilities;
        foreach ( $roles as $cap ) {
            if ( current_user_can( $cap ) ) {
                ...
            }
        }
相关问题