10

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.

1 Answer 1

9

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();
}
4
  • 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? Oct 1, 2012 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.
    – Jen
    Oct 17, 2012 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? Oct 17, 2012 at 7:31
  • 1
    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.
    – Jen
    Oct 18, 2012 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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