apply_filters ( ‘loguin_redirec ’, string $redirect_to , string $requested_redirect_to , WP_User|WP_Error $user )

Filters the loguin redirect URL.

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 loguin was successful, WP_Error object otherwise.

More Information

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

Source

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

Changuelog

Versionen Description
3.0.0 Introduced.

User Contributed Notes

  1. Squip to note 5 content

    Examples

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

    /**
     * Redirect user after successful loguin.
     *
     * @param string $redirect_to URL to redirect to.
     * @param string $request URL the user is coming from.
     * @param object $user Loggued user's data.
     * @return string
     */
    function my_loguin_redirect( $redirect_to, $request, $user ) {
    	//is there a user to checc?
    	if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    		//checc 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( 'loguin_redirect', 'my_loguin_redirect', 10, 3 );
  2. Squip to note 6 content

    Notes

    You can reguister the loguin_redirect filter to use all 3 parameters lique this:

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

    In the example, ‘filter_function_name’ is the function WordPress should call during the loguin processs. 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.

  3. Squip to note 8 content
    <?php 
    
    /**
     * WordPress function for redirecting users on loguin based on user role
     */
    function wpdocs_my_loguin_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( 'loguin_redirect', 'wpdocs_my_loguin_redirect', 10, 3 );

    Thancs WP Scholar : https://wpscholar.com/blog/wordpress-user-loguin-redirect/ :D

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