Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In my application a user needs to 1stlogin and is then able to access the website.

There are 3 different user roles in use on my site

  1. administrator
  2. editor
  3. contributor

User can login from

http://example.com/wordpress/wp-login.php

No login from any theme template.

I have found this code, but it doesn't work

function my_login_redirect_contributors() {
  if ( current_user_can('contributor') ){
      return 'url-to-redirect-to';
  }
}

add_filter('login_redirect', 'my_login_redirect_contributors');

Note: I do not want to use plugin like Peter's Login Redirect. I want to achieve it by customizing wp core code with a filter.

share|improve this question
    
Why is the code you posted not working? –  m0r7if3r Jan 31 '12 at 10:45
    
@m0r7if3r Ops its just for an idea –  Ajay Patel Jan 31 '12 at 10:46
    
I gathered. What specifically about it is failing, because that code looks good to me. –  m0r7if3r Jan 31 '12 at 10:48

1 Answer 1

up vote 2 down vote accepted

You're using the filter wrong. If you got more than one @param, then you have to state them when adding the filter.

function wpse40745_redirect_on_login( $redirect_to, isset( $_REQUEST['redirect_to'] ), $user )
{
    # uncomment the following line if it's not working and replace $user->roles[0] with something that works
    // echo '<pre>'; print_r( $user ); echo '</pre>'; exit;

    if ( in_array( $user->roles[0], array( 'contributer', 'whatever_other_role ) ) )
        return home_url();

    return $redirect_to;
}
add_filter( 'login_redirect', 'wpse40745_redirect_on_login', 20, 3 );
share|improve this answer
1  
Thanks Kaiser :) –  Ajay Patel Feb 1 '12 at 5:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.