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 would like to use the WP native login/register/restore password/.. functionality, but display pretty URLs instead of wp-login.php

I've been looking at the core code and noticed that most login related forms have got hard coded action URL. For example, the restore password form has got the following action URL

<form name="resetpassform" id="resetpassform" action="<?php echo esc_url( site_url( 'wp-login.php?action=resetpass&key=' . urlencode( $_GET['key'] ) . '&login=' . urlencode( $_GET['login'] ), 'login_post' ) ); ?>" method="post">

So, when I click 'Reset Password' button the information will be submitted to wp-login.php file with the specified action, key and login query variables.

If info doesn't pass validation then the form is redisplayed and the URL in the browser is the one that is specified in the form's action, so in this case it is http://example.com/wp-login.php?action=resetpass&key=xyz&login=zyx.

I would like it to be http://example.com/password-restore?action=resetpass&key=xyz&login=zyx

For this to work I've tried the following solution. However, I end up in the redirect loop.

Step 1. The initial restore password URL is http://example.com/wp-login.php?action=rp&key=xyz&login=zyx. To make it pretty I've added a re-write rule:

add_action( 'init', 'pretty_login_urls' );
function pretty_login_urls() {
    add_rewrite_rule( 'password-restore?([^/]+)/?$', "wp-login.php?$1", 'top' );
}

So, the initial URL to restore form is working.

Step 2. However, when I submit an un-matching password, the form is redisplayed and URL is now http://example.com/wp-login.php?action=resetpass&key=xyz&login=zyx. Notice, that action has changed.

Here is where I'm stack. I'd like to display that URL as pretty. I've tried the following solution trying to redirect wp-login.php to a pretty URL:

add_action( 'login_init', 'pretty_login_urls_form_action_fix' );
function pretty_login_urls_form_action_fix(){

    $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : '';
    if( in_array( $action, array('rp', 'resetpass') ) ){

        $url = home_url( '/password-restore?action='. $action .'&key=' . urlencode( $_GET['key'] ) . '&login=' . urlencode( $_GET['login'] ), 'login_post' );
        wp_redirect( $url );
        exit;
    }
}

However, with this I end up in the redirect loop.

Is there a way I can make the URLs display pretty in the browser address bar when the page is reached via a form's action URL.

I'd really appreciate any tips.

Many thanks, Dasha

share|improve this question

1 Answer 1

Step 2. However, when I submit an un-matching password, the form is redisplayed and URL is now http://example.com/wp-login.php?action=resetpass&key=xyz&login=zyx. Notice, that action has changed.

When I attempt this with an invalid key I get redirected to http://example.com/wp-login.php?action=lostpassword&error=invalidkey.

Anyway, the infinite loop is occurring because you are the condition in_array( $action, array('rp', 'resetpass') ) remains true for the url you are redirecting to, so you end up in a redirect loop.

The solution is to check that the requested url differs from the redirect url:

function pretty_login_urls_form_action_fix(){

    $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : '';
    if( in_array( $action, array('rp', 'resetpass') ) ){

        $requested_url  = is_ssl() ? 'https://' : 'http://';
        $requested_url .= $_SERVER['HTTP_HOST'];
        $requested_url .= $_SERVER['REQUEST_URI'];

        $redirect_url = home_url( '/password-restore?action='. $action .'&key=' . urlencode( $_GET['key'] ) . '&login=' . urlencode( $_GET['login'] ), 'login_post' );

        if( $redirect_url !== $requested_url ){         
            wp_redirect( $redirect_url );
            exit;
        }
    }
}
share|improve this answer

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.