8

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:

/** 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:

if ( !current_user_can( 'ma ...

when I modified the function, both contributors and admins are redirected to 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?

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.

1

2 Answers 2

23

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' ) ){}
4
  • 1
    I tried using 'administrator' instead of 'manage_options' but I get the same results. If I understand correctly the current_user_can() tag only applies to capabilities and not user roles.
    – ItsGeorge
    Nov 15, 2012 at 20:17
  • 1
    Its argument is either a capability or a role name, so it should work (codex.wordpress.org/Function_Reference/current_user_can).
    – C. E.
    Nov 15, 2012 at 22:42
  • 1
    So either way, the function doesn't work and the problem is that neither roles nor capabilities are being recognized by the function correctly. If I set it to an administrator specific role, or just 'administrator', it should use the 'if' url if I log in as an admin, and the 'else' url if I log in as a contributor. Either way, whether I set the function to 'current_user_can' or '!current_user_can' the administrator login and the contributor login behave the same as if the capabilities are not any different.
    – ItsGeorge
    Nov 16, 2012 at 3:13
  • 3
    Quote from the function definition: While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results. Oct 21, 2019 at 11:32
0

Try this:

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

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.