Codex

Plugin API/Filter Reference/login redirect

Description

The "login_redirect" filter is used to change the location redirected to after logging in. This could be the location set by the "redirect_to" parameter sent to the login page.

A plugin can register a login_redirect filter with the code:

<?php add_filter( 'login_redirect', 'filter_function_name' ) ?>

Where 'filter_function_name' is the function WordPress should call during the login process. Note that filter_function_name should be unique function name. It cannot match any other function name already declared.

The filter passes 3 optional parameters

  1. redirect_to (the URL to redirect to)
  2. request (the URL the user is coming from)
  3. user (an object containing the logged in user's data)

Note that the $current_user global may not be available at the time this filter is run. So you should use the $user global or the $user parameter passed to this filter.

You can register the login_redirect filter to use all 3 parameters like this:

<?php add_filter( 'login_redirect', 'filter_function_name', 10, 3 ) ?>

Example

This example redirects admins to the dashboard and other users to the homepage. Make sure you use add_filter outside of is_admin(), since that function is not available when the filter is called.

function my_login_redirect( $redirect_to, $request, $user ){
    //is there a user to check?
    if( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if( in_array( "administrator", $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    }
    else {
        return $redirect_to;
    }
}
add_filter("login_redirect", "my_login_redirect", 10, 3);

Redirect all logins to the homepage with an anonymous function (php 5.3+).

add_filter( 'login_redirect', create_function( '$url,$query,$user', 'return home_url();' ), 10, 3 );
This page is marked as incomplete. You can help Codex by expanding it.