problem with capabilities and current_user_can() (5 posts)

  1. gpspake
    Posted 2 years ago #

    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.

  2. tiaanswart
    Posted 2 years ago #

    Try using:

    if ( current_user_can( 'edit_posts' ) ) {

    instead of:

    if ( current_user_can( 'manage_options' ) ) {

  3. gpspake
    Posted 2 years ago #

    Thanks for the quick response.
    I get the same results when I use 'edit_posts'

  4. gpspake
    Posted 2 years ago #

    Someone in another forum suggested using 'administrator' instead of 'edit_posts' but this didn't work. If I understand correctly, current_user_can() only applies to capabilities and not user roles. Can anyone help me figure out why this won't work when I'm using a capability that is exclusive to admins?

  5. Mike Schinkel
    Posted 2 years ago #

    Try this maybe?

    function mysite_login_redirect( $redirect_url, $POST_redirect_url, $user ) {
      if ( is_a( $user, 'WP_User' ) ) {
        if ( $user->has_cap( 'manage_options' ) ) {
          $redirect_url = admin_url();
        } else {
          $redirect_url = admin_url('post-new.php');
      return $redirect_url;
    add_action( 'login_redirect', 'mysite_login_redirect', 10, 3 );

Topic Closed

This topic has been closed to new replies.

About this Topic