wordpress 在functions.php中的Wordpress功能和current_user_can()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13404284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:05:42  来源:igfitidea点击:

Wordpress capabilities and current_user_can() in functions.php

wordpress

提问by ItsGeorge

I have added a function to functions.php to redirect users to posts-new.php after login and it works. However, I only want this to happen if the user logging in is a contributor. So I added the following:

我在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');

In this state, both contributors and admins are redirected to post-new.php. To test it I modified the function so that users without the capability would be redirected:

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

if ( !current_user_can( 'ma ...

when I modified the function, both contributors and admins are redirected to index.php.

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

So the function seems to work but this implies to me that it's not seeing the 'manage_options' capability for admins. I've tried several admin-exclusive capabilities with the same results. Weird huh?

所以该功能似乎有效,但这对我来说意味着它没有看到管理员的“manage_options”功能。我尝试了几种管理员专有功能,结果相同。很奇怪吧?

I should say that I am using the user role-editor-plugin but I disabled it and tested the functions with the same results.

我应该说我正在使用用户角色编辑器插件,但我禁用了它并测试了具有相同结果的功能。

I'm also using Active Directory Integration and Admin Menu Editor.

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

回答by zourbuth

Try this:

尝试这个:

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

Or:

或者:

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' ) ){}

回答by Shivanand Sharma

Try this:

尝试这个:

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