Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress capabilities and current_user_can() in functions.php

Tags:

wordpress

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.

like image 453
ItsGeorge Avatar asked Nov 15 '12 18:11

ItsGeorge


1 Answers

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' ) ){}
like image 184
zourbuth Avatar answered Sep 19 '22 07:09

zourbuth