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.

I have a hard coded login/register/lost password form in the sidebar of my theme. I am using hidden input for "redirect_to" which is working fine. But when I press login button without entering anything or specifically when an error occurs, it redirects to wp-login.php page.

I dont want this to happen. If an error occurs it should show in the current place. How can i do this.

Note: Same is required for the logout function. Basically, I dont want to show wp-login.php page at any cost to anyone.

share|improve this question
    
Is this for security or cosmetic reasons? –  Wyck Aug 11 '11 at 16:58
    
Both. Basically, I am creating a custom theme for real estate portal. For that I dont want anything different from my theme. –  booota Aug 12 '11 at 4:52
add comment

2 Answers

The action wp_login_failed fires when there's a failed login due to a faulty username/password combination. So that's a good place to start. This is a super simple example that just redirects to the home page.

<?php
add_action( 'wp_login_failed', 'wpse25628_login_failed', 10, 1 );
/**
 * Catches all failed logins and redirect them to the 
 * websites home page.
 */
function wpse25628_login_failed( $username )
{
    /*
    May want to do something here to give the user feedback
    for instance call `global $user;`, which will be a
    wp_error object, you could append the code as a 
    $_GET variable and adjust your front end according to the
    error message
    */
    wp_redirect(
        home_url(),
        302
    );
    exit();
}

And, of course, there's a wp_logout hook as well! Maybe you send folks a thank you page here instead of the home page.

<?php
add_action( 'wp_logout', 'wpse25628_catch_logout' );
/**
 * When a user logs out, send them back to the home page
 */
function wpse25628_catch_logout()
{
    wp_redirect(
        home_url(),
        302
    );
    exit();
}

Unfortunately, the wp_login_failed hook does not fire if there's an empty username or password. Also equally unfortunate is the lack of hooks that get fired when a that happens. It's probably better to validate that sort of thing on the client side with javascript. Here's a quick example.

jQuery(document).ready(function(){
    jQuery('#loginform').submit(function(e){
        var errors;
        jQuery('#loginform input').each(function(){
            var val = jQuery(this).val()
            if( ! val )
            {   
                errors = true;
            }
        });
        if( errors )
        {
            e.preventDefault();
        }
    });
});
share|improve this answer
add comment

You can redirect away from wp-login and other default pages this way:

add_action('plugins_loaded', 'myAction');
function wpSimpleUserSetup() {
    if (in_array($_SERVER['REQUEST_URI'], array('/wp-login.php', 'wp-login.php?action=register'))) {
        wp_redirect(wp_login_url());
        exit;
    }
}

Moreover, the wp_login_url() function is pluggable so you can actually override it with your own:

function myLoginUrl( $redirect = '', $force_reauth = false ) {
    return $custom_url;
}
add_filter( 'login_url', 'myLoginUrl', 9999 );
share|improve this answer
    
downvote as the answer is just plain wrong. I edited the first one to let it work (WP globals are not available on plugins_loaded), but the pluggable function cannot work this way. –  kaiser Jan 19 '12 at 17:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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