Last active
August 3, 2023 15:59
-
-
Save JiveDig/dba4804504318facab1f to your computer and use it in GitHub Desktop.
Login form for template. Redirects back to login page with errors on fail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//* Put these in functions.php | |
/** | |
* Description: This redirects the failed login to the custom login page instead of default login page with a modified url | |
* @link ttp://wordpress.stackexchange.com/questions/110094/custom-login-redirects-to-wp-admin-on-wrong-password | |
**/ | |
add_action( 'wp_login_failed', 'prefix_front_end_login_fail' ); | |
function prefix_front_end_login_fail( $username ) { | |
// Getting URL of the login page | |
$referrer = $_SERVER['HTTP_REFERER']; | |
// if there's a valid referrer, and it's not the default log-in screen | |
if( !empty( $referrer ) && !strstr( $referrer,'wp-login' ) && !strstr( $referrer,'wp-admin' ) ) { | |
wp_redirect( $referrer . "?login=failed" ); | |
exit; | |
} | |
} | |
/** | |
* Description: This redirects to the custom login page if user name or password is empty with a modified url | |
* @link ttp://wordpress.stackexchange.com/questions/110094/custom-login-redirects-to-wp-admin-on-wrong-password | |
**/ | |
add_action( 'authenticate', 'prefix_check_username_password', 1, 3); | |
function prefix_check_username_password( $login, $username, $password ) { | |
// Getting URL of the login page | |
$referrer = $_SERVER['HTTP_REFERER']; | |
// if there's a valid referrer, and it's not the default log-in screen | |
if( !empty( $referrer ) && !strstr( $referrer,'wp-login' ) && !strstr( $referrer,'wp-admin' ) ) { | |
if( $username == "" || $password == "" ){ | |
wp_redirect( $referrer . "?login=empty" ); | |
exit; | |
} | |
} | |
} | |
//* Put this in your login page template | |
/* | |
* Show login page with errors when fail | |
* @link ttp://wordpress.stackexchange.com/questions/110094/custom-login-redirects-to-wp-admin-on-wrong-password | |
*/ | |
add_action( 'genesis_entry_content', 'prefix_do_login_form' ); | |
function prefix_do_login_form() { | |
if ( is_user_logged_in() ) { | |
echo '<span style="display:block;text-align:center;">You are logged in.</span>'; | |
} else { | |
if ( isset( $_GET['login'] ) && $_GET['login'] == 'failed' ) { | |
echo '<div class="wp_login_error">'; | |
echo '<p>The password you entered is incorrect, Please try again.</p>'; | |
echo '</div>'; | |
} elseif ( isset( $_GET['login'] ) && $_GET['login'] == 'empty' ) { | |
echo '<div class="wp_login_error">'; | |
echo '<p>Please enter both username and password.</p>'; | |
echo '</div>'; | |
} | |
$args = array( | |
'echo' => true, | |
'redirect' => site_url( $_SERVER['REQUEST_URI'] ), | |
'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' => false | |
); | |
wp_login_form( $args ); | |
} | |
} | |
//* To redirect the WP login page use this https://gist.github.com/JiveDig/adedc27cc2d9320ce6de |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment