• 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 'https://mysite.com/wp-admin/index.php';}
    	else {
    		return 'https://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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try using:

    if ( current_user_can( 'edit_posts' ) ) {

    instead of:

    if ( current_user_can( 'manage_options' ) ) {

    Thread Starter gpspake

    (@gpspake)

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

    Thread Starter gpspake

    (@gpspake)

    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?

    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 );
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘problem with capabilities and current_user_can()’ is closed to new replies.