Skip to content
  • Log In
  • Register
WordPress.org
  • News
  • Download & Extend
    • Get WordPress
    • Themes
    • Patterns
    • Plugins
    • Openverse
    • Mobile
    • Hosting
  • Learn
    • Learn WordPress
    • Documentation
    • Forums
    • WordPress.tv
    • Developers
  • Community
    • Make WordPress
    • WordCamp
    • Meetups
    • Photo Directory
    • Job Board
    • Five for the Future
  • About
    • About WordPress
    • Showcase
    • Gutenberg
    • Enterprise
  • Get WordPress
Get WordPress

Developer Resources

Browse: Home / Reference / Hooks / login_redirect

apply_filters( 'login_redirect', string $redirect_to, string $requested_redirect_to, WP_User|WP_Error $user )

Filters the login redirect URL.

Contents

  • Parameters
  • More Information
  • Source
  • Changelog
  • User Contributed Notes
    • Feedback
    • Feedback

Parameters

$redirect_to string
The redirect destination URL.
$requested_redirect_to string
The requested redirect destination URL passed as a parameter.
$user WP_User|WP_Error
WP_User object if login was successful, WP_Error object otherwise.

Top ↑

More Information

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.


Top ↑

Source

File: wp-login.php. View all references

$redirect_to = apply_filters( 'login_redirect', $redirect_to, $requested_redirect_to, $user );

View on Trac View on GitHub


Top ↑

Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    You must log in to vote on the helpfulness of this noteVote results for this note: 12You must log in to vote on the helpfulness of this note
    Contributed by Niels Lange — 6 years ago

    Examples

    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.

    /**
     * Redirect user after successful login.
     *
     * @param string $redirect_to URL to redirect to.
     * @param string $request URL the user is coming from.
     * @param object $user Logged user's data.
     * @return string
     */
    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 );
    Log in to add feedback
  2. Skip to note 2 content
    You must log in to vote on the helpfulness of this noteVote results for this note: 3You must log in to vote on the helpfulness of this note
    Contributed by Niels Lange — 6 years ago

    Notes

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

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

    In the example, ‘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 $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.

    Log in to add feedback
  3. Skip to note 3 content
    You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note
    Contributed by Dhimas Kirana — 3 years ago
    <?php 
    
    /**
     * WordPress function for redirecting users on login based on user role
     */
    function wpdocs_my_login_redirect( $url, $request, $user ) {
        if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
            if ( $user->has_cap( 'administrator' ) ) {
                $url = admin_url();
            } else {
                $url = home_url( '/members-only/' );
            }
        }
        return $url;
    }
    
    add_filter( 'login_redirect', 'wpdocs_my_login_redirect', 10, 3 );

    Thanks WP Scholar : https://wpscholar.com/blog/wordpress-user-login-redirect/ :D

    Top ↑

    Feedback

    • $user->has_cap() to check a role is discouraged as it may produce unreliable results. — By Chad Butler — 2 years ago

    Log in to add feedback
  4. Skip to note 4 content
    You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note
    Contributed by Steven Lin — 2 years ago

    Example Migrated from Codex:

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

    add_filter( 'login_redirect', function( $url, $query, $user ) {
    	return home_url();
    }, 10, 3 );

    Top ↑

    Feedback

    • Super useful, thank you! — By MetalMusicMan — 11 months ago

    Log in to add feedback

You must log in before being able to contribute a note or feedback.

  • About
  • News
  • Hosting
  • Donate
  • Swag
  • Documentation
  • Developers
  • Get Involved
  • Learn
  • Showcase
  • Plugins
  • Themes
  • Patterns
  • WordCamp
  • WordPress.TV
  • BuddyPress
  • bbPress
  • WordPress.com
  • Matt
  • Privacy
  • Public Code
WordPress.org
WordPress.org
  • Visit our Facebook page
  • Visit our Twitter account
  • Visit our Instagram account
  • Visit our LinkedIn account
Code is Poetry

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.