0

I am using a custom login page for my site, which also includes redirections for the custom login page and user management.

however, when the auth check modal appears, it loads the dashboard inside the iframe, rather than closing it as it does with wp-login.php

Not quite clear on a solution for this. I tried hooking onto wp_login action to fire javascript to hide it, but this did not work (for reasons I am not clear on, probably blocked for secturiy or something)

in functions.php:

add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect($redirect_to, $requested_redirect_to, $user) {
   if (is_wp_error($user)) {
       //Login failed, find out why...
       $error_types = array_keys($user->errors);
       //Error type seems to be empty if none of the fields are filled out
       $error_type = 'both_empty';
       //Otherwise just get the first error
        $login_url = home_url( '/login' );
       if (is_array($error_types) && !empty($error_types)) {
           $error_type = $error_types[0];
           $_SESSION["err_codes"] = $error_type;
       }
       wp_redirect( $login_url  . "?login=failed&reason=" . $error_type ); 
       return $error_type;
       exit;
   } else {
       //Login OK - redirect to another page?
        wp_safe_redirect( admin_url() );
        echo '<script type="text/css">
       #wp-auth-check-wrap{
       display:none;
       }
       </script>';
   }
}

function user_logout(){
   $login_page  = home_url( '/login' );
   session_unset();
   session_destroy();
   wp_redirect( $login_page . '?loggedout=true');
   exit;
}
add_action( 'wp_logout', 'user_logout' );


/*
function cookie_expiration( $expiration, $user_id, $remember ) {
   return $remember ? $expiration : 10;
}
add_filter( 'auth_cookie_expiration', 'cookie_expiration', 99, 3 );
*/

// custom errors for login page

function error_handling(){

$err_codes = isset($_SESSION["err_codes"]) ? $_SESSION["err_codes"] : 0;

switch ($err_codes) {

 case "incorrect_password":
    echo "Sorry, your password was not correct";
   break;
 case "invalid_username":
   echo "Sorry, your username was not correct";
   break;
 case "invalid_email":
      echo "Sorry, your email address was not correct";
   break;
     case "empty_username":
   echo "Please enter your username";
   break;
     case "empty_password":
      echo "Please enter your password";
   break;
 default:
   echo "Please enter your credentials";
   }
}

add_action( 'custom_errors', 'error_handling' );

and the login form code (template file):

<?php
/**
* Template Name: Custom Login Page
*/

get_header();

if ( ! is_user_logged_in() ) { 
$args = array(
        'echo' => true,
        'form_id' => 'loginform',
        'label_username' => __( 'Username' ),
        'label_password' => __( 'Password' ),
        'label_remember' => __( 'Remember Me' ),
        'label_log_in' => __( 'Log In' ),
        'id_username' => 'user_login',
        'id_password' => 'user_pass',
        'id_remember' => 'rememberme',
        'id_submit' => 'wp-submit',
        'remember' => true,
        'value_username' => NULL,
        'value_remember' => true,
);

}

else {
    return;
}


?>

<style>
    .login-username {
    display: flex;
}
</style>
<div class="login-container">
<div class="login-form">
<?php

wp_login_form($args);

do_action( 'custom_errors' );

?>
</div>
</div>

auth check modal

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.