Filter uncanny-learndash-toolkit

login_redirect

This filter is documented in wp-login.php */ Filters the redirect destination after a user successfully logs in.

add_filter( 'login_redirect', $callback, 10, 2 );

Description

Filters the URL users are redirected to after logging in. Modify this filter to customize the login redirect destination based on user roles, capabilities, or other conditions. The original redirect URL and the requested redirect URL are also provided for advanced control.


Usage

add_filter( 'login_redirect', 'your_function_name', 10, 2 );

Parameters

$redirect_to (mixed)
- **$requested_redirect_to** `mixed`
$new_user (mixed)

Return Value

The filtered value.


Examples

<?php
/**
 * Redirect administrators to the WordPress dashboard after login,
 * and other users to a custom 'members' page.
 *
 * @param string $redirect_to The redirect destination URL.
 * @param string $requested_redirect_to The URL the user was trying to access before login.
 * @param WP_User|bool $new_user True if the user is new, false if not.
 * @return string The updated redirect destination URL.
 */
add_filter( 'login_redirect', function( $redirect_to, $requested_redirect_to, $new_user ) {

    // If the user is already specified a redirect, use that.
    // This handles cases where a user tries to access a restricted page before logging in.
    if ( ! empty( $requested_redirect_to ) ) {
        $redirect_to = $requested_redirect_to;
    }

    // If this is a new user registration, maybe redirect them to a welcome page.
    if ( $new_user ) {
        // For this example, we won't do anything special for new users and will let the default logic handle it.
        // In a real scenario, you might do something like:
        // $redirect_to = home_url( '/welcome-new-user/' );
    } else {
        // If it's an existing user and they are an administrator
        if ( current_user_can( 'administrator' ) ) {
            // Redirect administrators to the dashboard
            $redirect_to = admin_url();
        } else {
            // Redirect other users to a custom 'members' page
            $redirect_to = home_url( '/members/' );
        }
    }

    return $redirect_to;

}, 10, 3 ); // 10 is the default priority, 3 is the number of arguments the callback accepts.

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/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-legacy.php:374
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-legacy.php:379
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-legacy.php:724
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-2.6.php:451
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-2.6.php:456
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-2.6.php:861
src/classes/frontend-login-plus.php:2215
src/classes/frontend-login-plus.php:3406
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-2-4.php:406
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-2-4.php:411
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-2-4.php:800
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-2-3.php:379
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-2-3.php:384
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-2-3.php:769
src/includes/user-switching.php:378
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-2-5.php:405
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-2-5.php:410
src/includes/two-factor/providers/wp-2fa/legacy/class-frontend-login-plus-2fa-2-5.php:799

}

		if ( ! $new_user ) {
			/** This filter is documented in wp-login.php */
			$redirect_to = apply_filters( 'logout_redirect', $redirect_to, $requested_redirect_to, $old_user );
		} else {
			/** This filter is documented in wp-login.php */
			$redirect_to = apply_filters( 'login_redirect', $redirect_to, $requested_redirect_to, $new_user );
		}

		/**
		 * Filters the redirect location after a user switches to another account or switches off.
		 *
		 * @since 1.7.0
		 *


Scroll to Top