Make WordPress Core

Opened 7 weeks ago

Last modified 7 weeks ago

#61746 new defect (bug)

WordPress login redirect issue

Reported by: dipakparmar2007's profile dipakparmar2007 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: critical Version: 6.6.1
Component: General Keywords:
Focuses: Cc:

Description

if ( isset( $_POSTaction? ) && $_POSTaction? == 'log-in' ) {

$user_login = $_POSTuser_login?;
$password = $_POSTpassword?;
$user_data = array(

'user_login' => $user_login,
'user_password' => $password,
'remember' => true

);

$user = wp_signon( $user_data );

if ( ! is_wp_error( $user ) ) {

$user_id = $user->ID;

if ( $user_id ) {

$url = site_url().'/dashboard/';

wp_redirect( $url );
exit;

}

} else {

$error_message = $user->get_error_message();

}

}

<form action="" method="post" class="sign-in">

<div class="form_group">

<label for="user_login"><?php _e('Username'); ?></label>


<input type="text" name="user_login" class="input_control" id="user_login" value="" />

</div>
<div class="form_group">

<label for="password"><?php _e('Password'); ?></label>
<input type="password" class="input_control" name="password" id="password" />

</div>
<div class="forgot"><a href="<?php echo site_url(); ?>/forgot-password/">Forgot Password?</a></div>
<div class="form_footer">

<button type="submit"><?php _e('Log in'); ?></button>

<input type="hidden" name="action" value="log-in" />

</div>

</form>

Attachments (1)

login-issue.jpg (130.9 KB) - added by dipakparmar2007 7 weeks ago.

Download all attachments as: .zip

Change History (2)

#1 @dipakparmar2007
7 weeks ago



// Hook into the login_redirect filter
add_filter('login_redirect', 'custom_login_redirect', 10, 3);

function custom_login_redirect($redirect_to, $request, $user) {
    // Check if the user object is a WP_User instance
    if (isset($user->roles) && is_array($user->roles)) {
        // Check user role and redirect accordingly
        if (in_array('administrator', $user->roles)) {
            // Redirect administrators to the admin dashboard
            return home_url('/dashboard/');
        } else {
            // Redirect other users to a specific page
            return home_url('/dashboard/'); // Change /custom-page to your desired page
        }
    } else {
        // Default redirect
        return home_url('/dashboard/');
    }
}

This hook is ilso not work in WordPress version 6.1.1 and I set default themes.

Version 0, edited 7 weeks ago by dipakparmar2007 (next)
Note: See TracTickets for help on using tickets.