• About WordPress
    • About WordPress
    • WordPress.org
    • Documentation
    • Support
    • Feedback
  • Log In
  • Register

WordPress.org

  • Showcase
  • Learn
  • Themes
  • Plugins
  • Mobile
  • Support
    • Documentation
    • Forums
  • Get Involved
    • Five for the Future
  • About
  • Blog
  • Hosting
  • Get WordPress

Code Reference

Skip to content
Filter by type:
Search
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
  • Source
  • Changelog
  • User Contributed Notes

Parameters #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 ↑

Source #Source

File: wp-login.php

View on Trac


Top ↑

Changelog #Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes #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: 9You must log in to vote on the helpfulness of this note
    Contributed by Niels Lange — 4 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 );
    

    Expand full source codeCollapse full source code

    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: 2You must log in to vote on the helpfulness of this note
    Contributed by Niels Lange — 4 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: 2You must log in to vote on the helpfulness of this note
    Contributed by Dhimas Kirana — 9 months 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 );
    

    Expand full source codeCollapse full source code

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

    Feedback

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

    Log in to add feedback

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

  • About
  • Blog
  • Hosting
  • Donate
  • Support
  • Developers
  • Get Involved
  • Learn
  • Showcase
  • Plugins
  • Themes
  • WordCamp
  • WordPress.TV
  • BuddyPress
  • bbPress
  • WordPress.com
  • Matt
  • Privacy
  • Public Code
  • @WordPress
  • WordPress

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.