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'm providing front-end login in a template, using the function wp_login_form. It handles the redirection after login and logout pretty neatly! But if the username / password mismatch, or are non existent, the user is redirected to the admin login page, with the shaky js error. Also same is the case when I'm trying to reset the password, it takes me to the me backend page where I have to enter my email for resetting the password. I want to show both these activities on my template. Is there a way I could do this?

Thanks for any help in advance.

share|improve this question
add comment

1 Answer 1

up vote 6 down vote accepted

Yes it's possible. You have to create three filters for this.

Admin stuff inside a class

class WPSE29338_Admin {
    public static function setup() {
        add_filter('login_url', array(__CLASS__, 'modifyLoginURL'), 10, 2);
        add_filter('lostpassword_url', array(__CLASS__, 'modifyLostPasswordURL'), 10, 2);
        add_filter('register', array(__CLASS__, 'modifyRegisterURL'));
    }

    public static function modifyLoginURL($loginUrl, $redirect = '') {
        $loginUrl = site_url('login'); // Link to login URL

        if(!empty($redirect)) {
            $loginUrl = add_query_arg('redirect_to', urlencode($redirect), $loginUrl);
        }

        return $loginUrl;
    }

    public static function modifyLostPasswordURL($lostpwUrl, $redirect = '') {
        $lostpwUrl = wp_login_url() . '#lostpassword'; // Link to lostpassword URL

        if(!empty($redirect)) {
            $lostpwUrl = add_query_arg('redirect_to', urlencode($redirect), $lostpwUrl);
        }

        return $lostpwUrl;
    }

    public static function modifyRegisterURL($registerUrl) {
        if(!is_user_logged_in()) {
            if (get_option('users_can_register')) {
                $registerUrl = '<a href="' . wp_login_url() . '#register" class="btn">' . __('Register') . '</a>'; // Link to register URL
            } else {
                $registerUrl = '';
            }
        }

        return $registerUrl;
    }
}

First we need to filter the output of the function wp_login_url() which is used by wp_login_form() in the forms action attribute.

Look at the method modifyLoginUrl(). Here we store the URL of the page login inside the variable $loginUrl. This page must exists inside WordPress, so create it first.

Next we need a filter for the function wp_lostpassword_url() and wp_register(). It's basically the same. The URL of the site is stored inside the variable $lostpwUrl and $registerUrl.

Last but not least, you have to call the class on the right action hook. For themes this should be after_setup_theme and for plugins you can use plugins_loaded.

Themes / functions.php

add_action('after_setup_theme', 'wpse29338_admin_init');
function wpse29338_admin_init() {
    WPSE29338_Admin::setup();
}

Plugins

add_action('plugins_loaded', 'wpse29338_admin_init');
function wpse29338_admin_init() {
    WPSE29338_Admin::setup();
}
share|improve this answer
    
Thanks a lot @Roman... I'm trying this out today. –  Rutwick Gangurde Sep 27 '11 at 3:29
    
I'm sorry to bump this, but I would love to do this, but I can't seem to figure it out! In the code you provided, what should be changed by me? –  Bram Vanroy Oct 1 '12 at 15:17
    
Bram, if you're creating a custom theme, then put all of the first and second blocks of code in your theme functions file. If you're creating a plugin, then put the first and third blocks of code in your plugin file. It looks like the only thing you might have to modify is the 9th line of the class, where it says $loginUrl = site_url('login'); // Link to login URL. Just change 'login' to match the slug of the login page you'd like to use. Unless Rofflox made a syntax error that I'm not seeing, this looks like it'd work beautifully. –  guiniveretoo Oct 17 '12 at 1:16
    
@guiniveretoo Thanks for your reply! The problem is that I work with a dropdown form that can be accessed from any page on the website. So, there is no definite slug possible, because people can login from any page ... So, what should I put there? –  Bram Vanroy Oct 17 '12 at 7:31
    
Bram - Wordpress needs a login url so that if there's some user error or similar, there's somewhere for them to go and reset their password or sign up for an account. Setting a login url provides that page. I did exactly this setup with a project last year: a javascript login box that appeared with the login form - if they enter their info correctly, it sends them to wp-admin; if not, they end up on the custom login page to either try again or reset their password. –  guiniveretoo Oct 18 '12 at 18:05
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.