Leave a Reply

1 comment

  1. Proper hooks for login and register actions:

    <?php
    function custom_plugin_hooks() {
        add_action( 'login_form', 'custom_plugin_form_inputs' );
        add_filter( 'authenticate', 'custom_plugin_login_check', 100, 3 );
        add_action( 'register_form', 'custom_plugin_form_inputs' );
        add_action( 'register_post', 'custom_plugin_registration_check', 100, 3);
    }
    custom_plugin_hooks();
    
    function custom_plugin_form_inputs() {
        echo "n".'<p>';
        echo '<label>Custom input: <br />';
        echo '<input type="text" name="custom-input" class="input" value="" />';
        echo '</label></p>'."n";
    }
    
    function custom_plugin_login_check($user, $username, $password) {
        // user gave us valid username and password
        if( !is_wp_error( $user ) ) {
            if( !empty( $_POST ) ) {
                if( $_POST['custom-input'] !== 'custom-value' ) {
                    $error = new WP_Error();
                    $error->add( 'custom-login-error', 'Login error.');
                    return $error;
                }
            }
        }
        return $user;
    }
    
    function custom_plugin_registration_check( $login, $email, $errors ) {
        if( !empty( $_POST ) ) {
            if( $_POST['custom-input'] !== 'custom-value' ) {
                $errors->add( 'custom-registration-error', 'Registration error.');
            }
        }
        return $errors;
    }
    ?>