Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Possible Duplicate:
How to show 'login error' and 'lost password' on my template page?

I've created a custom login form in a template page using the basic wp_login_form. I'm using custom functions to prevent any access to the original wp-login.php.

My custom login doesn't handle errors unlike the default login page. Do you have simple solution to display them (wrong username/password, blank field, etc.)?

Thanks.

share|improve this question

marked as duplicate by toscho Oct 6 '12 at 11:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

Assuming you are using wp_signon, simply check if the returned object is a WP_Error instance, evaluate the WP_Error::get_error_code(), and output appropriate HTML/CSS that your form needs to flag errors.

Basic, untested sample code to illustrate the principle:

<?php
$user = wp_signon();
$loginError = $user instanceof WP_Error;
?>

<input name='log' type='text' />
<?php
if($loginError && $user->get_error_code() === 'invalid_username')
{
    echo $user->get_error_message();
}
?>

<input name='pwd' type='password' />
<?php
if($loginError && $user->get_error_code() === 'incorrect_password')
{
    echo $user->get_error_message();
}
?>
share|improve this answer

I'm using this kind of code which seems to work (with wp_signon).

<?php
/*
Template Name: Connexion
*/
global $user_ID;

if (!$user_ID) {

    if($_POST){

        $username = $wpdb->escape($_REQUEST['username']);
        $password = $wpdb->escape($_REQUEST['password']);
        $remember = $wpdb->escape($_REQUEST['rememberme']);

        if($remember) $remember = "true";
        else $remember = "false";
        $login_data = array();
        $login_data['user_login'] = $username;
        $login_data['user_password'] = $password;
        $login_data['remember'] = $remember;
        $user_verify = wp_signon( $login_data, false ); 

        if ( is_wp_error($user_verify) ) 
        {
           echo "<p class='error'>Identifiant ou mot de passe incorrect. Merci d'essayer &agrave; nouveau !</p>";
           exit();
         } else 
         {  
            echo "<script type='text/javascript'>window.location='". get_bloginfo('url') ."'</script>";
            exit();
          }
    } else { 

get_header();

?>

<div id="content" role="main">
<section id="page" class="alignleft">

    <!-- Si déjà connecté, redirection vers accueil -->
    <?php if (is_user_logged_in()) { ?>
        <?php wp_redirect( 'http://letouriste.fr/', 302 ); exit; ?>
    <?php } else { ?>

    <!-- Si pas connecté, formulaire de connexion -->
    <div class="alignleft">
        <h2 class="section-title alignleft">
            <span class="title"><?php the_title(); ?></span>
        </h2>

        <!-- Affichage des erreurs si besoin -->        
        <div id="result"></div> 

        <form method="post" id="loginform" name="loginform">
            <p class="login-username">
                <label for="user_login">Identifiant</label>
                <input type="text" tabindex="10" size="20" value="" name="username" class="input" id="user_login" name="log">
            </p>
            <p class="login-password">
                <label for="user_pass">Mot de passe</label>
                <input type="password" tabindex="20" size="20" value="" name="password" class="input" id="user_pass" name="pwd">
            </p>
            <p class="login-remember"><label><input type="checkbox" tabindex="90" value="forever" id="rememberme" name="rememberme"> Se souvenir de moi</label></p>
            <p class="login-submit">
                <input type="submit" tabindex="100" value="Se connecter" class="button-primary" id="wp-submit" name="wp-submit">
                <input type="hidden" value="http://letouriste.fr/" name="redirect_to">
            </p>
        </form>     

        <!-- Script de vérification des erreurs --> 
        <script type="text/javascript">                         
        $("#wp-submit").click(function() {

        $('#result').html('<img src="<?php bloginfo('template_url'); ?>/images/loading.gif" class="loader" />').fadeIn();
        var input_data = $('#loginform').serialize();
        $.ajax({
        type: "POST",
        url:  "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
        data: input_data,
        success: function(msg){
        $('.loader').remove();
        $('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
        }
        });
        return false;

        });
        </script>

    </div>

    <!-- Lien de création de compte --> 
    <div class="alignright">

        <h2 class="section-title alignleft">
            <span class="title">S'enregistrer</span>
        </h2>

        <p>Toujours pas inscrit ? <a title="Créer un compte Le Touriste" href="http://letouriste.fr/enregistrement/">Cr&eacute;er un compte gratuitement</a> en 30 secondes chrono !</p>

    </div>

    <?php } ?>

</section>

</div>

<?php get_footer(); } } else {
echo "<script type='text/javascript'>window.location='". get_bloginfo('url') ."'</script>";

}
?>
share|improve this answer
    
Could you reduce it a little bit, take the parts out that are not needed to answer the question and add some explanation? Oh, and welcome to WordPress Stack Exchange! –  toscho Oct 6 '12 at 12:01
    
Yep not the first time here but not very comfortable with Stack Exchange. :) Here is the tutorial I followed: wparena.com/how-to/… Although it seems to works fine, I have another problem... so another thread, right? –  Aurélien Oct 6 '12 at 12:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.