I've added a custom input field with login_form
hook.
So, I need to process the post data to validate.
I've used the lostpassword_post
for lostpassword_form
and works great, but I cant find a hook to validate my data at login form.
Any Ideas?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityUse the action 'login_init'
to catch all calls to wp-login.php
.
Sample code:
add_action( 'login_init', 'wpse_51227_validate_custom_field' );
function wpse_51227_validate_custom_field()
{
if ( ! isset ( $_POST['special_custom'] )
return;
if ( ! is_numeric( $_POST['special_custom'] )
// handle the error
}
There are two important global variables available:
$errors
is an instance of WP_Error
. Maybe you want to use it to store error data.$action
is the current action. Maybe you want to rest it to login
to prevent a redirect if the user input is invalid.Haven't really tested this but I'm just guessing by how I do it with the registration & profile pages.
Try something like this:
function my_login_errors( $errors ) {
if ( empty( $_POST['custom_field'] ) )
$errors->add( 'empty_custom_field', '<strong>ERROR</strong>: Please enter some info.' );
return $errors;
}
add_filter( 'login_errors', 'my_login_errors' );
Note: Modify the custom_field
part and the message accordingly.