login_redirect

最后更新于:2021-11-27 04:45:11

apply_filters( ‘login_redirect’, string $redirect_to, string $requested_redirect_to, GC_User|GC_Error $user )

Filters the login redirect URL.

参数

$redirect_to

(string)
The redirect destination URL.

$requested_redirect_to

(string)
The requested redirect destination URL passed as a parameter.

$user

(GC_User|GC_Error)
GC_User object if login was successful, GC_Error object otherwise.

源文件

文件: gc-login.php

View on Trac

/**
 * 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 );

<?php 

/**
 * GeChiUI function for redirecting users on login based on user role
 */
function gcdocs_my_login_redirect( $url, $request, $user ) {
    if ( $user && is_object( $user ) && is_a( $user, 'GC_User' ) ) {
        if ( $user->has_cap( 'administrator' ) ) {
            $url = admin_url();
        } else {
            $url = home_url( '/members-only/' );
        }
    }
    return $url;
}

add_filter( 'login_redirect', 'gcdocs_my_login_redirect', 10, 3 );