Wordpress capabilities and current_user_can() in functions.php
Asked Answered
G

3

9

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.

Gemmation answered 15/11, 2012 at 18:59 Comment(1)
More details can be found here codex.wordpress.org/Function_Reference/current_user_canOrangeman
P
24

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' ) ){}
Parquet answered 15/11, 2012 at 19:9 Comment(4)
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.Gemmation
Its argument is either a capability or a role name, so it should work (codex.wordpress.org/Function_Reference/current_user_can).Bedfast
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.Gemmation
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.Oudh
O
0

Try this:

$exclude_role = 'contributor';
        $roles = get_role( $exclude_role )->capabilities;
        foreach ( $roles as $cap ) {
            if ( current_user_can( $cap ) ) {
                ...
            }
        }
Oudh answered 21/10, 2019 at 11:35 Comment(0)
S
0

You are returning a value in an action.

login_redirect is a filter, so you should change add_action to add_filter.

Shufu answered 30/7 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.