Hi I’m trying to get my WordPress custom login which is in a dropdown in header.php to display errors when an incorrect email or password is entered, or even if both or 1 is left blank.
Here is the login form I’m using
<?php
if ( ! is_user_logged_in() ) { // Display WordPress login form:
$args = array(
'redirect' => admin_url(),
'form_id' => 'loginform-custom',
'label_username' => __( 'Username custom text' ),
'label_password' => __( 'Password custom text' ),
'label_remember' => __( 'Remember Me custom text' ),
'label_log_in' => __( 'Log In custom text' ),
'remember' => true
);
wp_login_form( $args );
} else { // If logged in:
wp_loginout( home_url() ); // Display "Log Out" link.
echo " | ";
wp_register('', ''); // Display "Site Admin" link.
}
?>
and I have found this code from here:
https://wordpress.stackexchange.com/questions/61267/prevent-wp-login-form-from-redirecting-to-wp-admin-when-there-are-errors
function wp_authenticate($username, $password) {
$username = sanitize_user($username);
$password = trim($password);
$user = apply_filters('authenticate', null, $username, $password);
if ( $user == null ) {
$user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
}
$ignore_codes = array('empty_username', 'empty_password');
if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
// Put your code here
}
return $user;
}
I’ve copied the above into functions.php in my theme folder but it’s not working – do I have to call it in my form, if so how? And what code should I put where it says:
// Put your code here?
The function wp_authenticate($username, $password) is a WP function so there is no need to add it in functions.php. It can be found in pluggable.php in the wp-includes directory.
And yes, you have to call it in your form or the validation script, in the same way you call any other function. For example:
I would guess you should call it before calling is_user_logged_in(), otherwise there won’t be any $username or $password as WP has already rejected the user.
Hope this helps.
Put the following code in
functions.php
if user click on login button without filling user name & password it will move towp-login.php
page.We can solve this buy writing the following code in
functions.php
file.wp_signon() function using custom login and its work fine for me :