Filter uncanny-learndash-toolkit

uo_do_login_redirect

Filters the login redirect URL, allowing modification of the destination after a user successfully logs in.

add_filter( 'uo_do_login_redirect', $callback, 10, 1 );

Description

Allows developers to modify the login redirect URL. By default, it returns true, enabling the default redirect behavior. Developers can return a custom URL string to override the default redirection logic after a successful login.


Usage

add_filter( 'uo_do_login_redirect', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Filter 'uo_do_login_redirect' to conditionally disable the login redirect.
 *
 * This callback checks if a specific user role is present and, if so,
 * prevents the login redirect from being applied.
 *
 * @param bool $should_redirect The default value, true, indicating the redirect should occur.
 * @return bool False if the user has the 'special_role', true otherwise.
 */
add_filter( 'uo_do_login_redirect', function( $should_redirect ) {
    // Get the currently logged-in user object.
    $current_user = wp_get_current_user();

    // Check if the user is logged in and has the role 'special_role'.
    if ( $current_user && in_array( 'special_role', (array) $current_user->roles ) ) {
        // If the user has the 'special_role', disable the login redirect.
        return false;
    }

    // Otherwise, allow the login redirect to proceed.
    return $should_redirect;
}, 10, 1 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/login-redirect.php:43

public static function run_frontend_hooks() {

		if ( true === self::dependants_exist() ) {
			$redirect_priority = 999;
			
			$settings = get_option( 'LoginRedirect', Array() );
			if ( ! empty( $settings ) ) {
				foreach ( $settings as $setting ) {
					
					if ( 'redirect_priority' === $setting['name'] ) {
						$redirect_priority = $setting['value'];
					}
				}
			}
			if ( empty( $redirect_priority ) ) {
				$redirect_priority = 999;
			}
			
			$do_login_redirect = apply_filters( 'uo_do_login_redirect', true );
			if( $do_login_redirect ){
				add_filter( 'login_redirect', array( __CLASS__, 'login_redirect' ), $redirect_priority, 3 );
			}

			$do_logout_redirect = apply_filters( 'uo_do_logout_redirect', true );
			if( $do_logout_redirect ){
				add_action( 'wp_logout', array( __CLASS__, 'logout_redirect' ), 1, 1 );
			}
		}

	}


Scroll to Top