Support » Developing with WordPress » How to use the redirect_to method properly on a custom login page?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator t-p


    Hey @t-p, i’m fairly new to the wordpress & php world. Am i supposed to copy and paste the entire source of the login_redirect code on my login.php page? I’m not using the general wp-login.php I’m only using require( dirname(__FILE__) . '/wp-load.php' ); so i can call wordpress global login variables.

    Moderator bcworkz


    If you have a custom login page, don’t bother with the “login_redirect” filter unless you need it for compatibility reasons. In that case you wouldn’t add a hook, but apply the filter in your login code. Then other code can alter the redirect destination through the hook. If there is no such other code, there is not point in applying the hook. It’s more of a concern if you are developing code to be distributed for other installations besides your own.

    I don’t see any issue in your redirect code, the problem may lie in the redirect URL parameter. It might be rejected by the safe redirect script, but that wouldn’t normally take one to the home page unless the login failed. After the redirect to home, is the login successful? Can you then navigate to /wp-admin/ without a login prompt?

    It’s possible some other plugin or your theme is redirecting your redirect. Try deactivating all plugins and switching to a default twenty* theme. Be sure any code related to you custom login is still active some how. In this near default state, try logging in with a redirect URL parameter. Be sure the redirect URL is valid and it has been URL encoded with PHP’s urlencode() or equal. (characters like : and / are converted to %hexcode forms)

    If you still have trouble, try using wp_redirect() instead. Just as a test, safe redirect is what you ultimately want to use.

    You might consider using wp_signon() instead of checking the password. Then you don’t need to fuss with cookies, it’s handled for you. Just verify the return is a WP_User object and not WP_Error. Most importantly, wp_signon() is more compatible with other code. Just checking the password introduces some compatibility issues. It may not be an issue on your site, but it could be.

    Speaking of compatibility, requiring wp-load.php is an incompatible way to load the WP environment. If it works on your site, then that’s great. It will not work on all WP installations though. Just sayin’ 🙂

    @bcworkz thank you for the very informative reply. The logins are successful but the wp_safe redirect doesn’t seem to work. I’m thinking If I should do a javascript redirect after a successful login?

    $redir = '<script>window.location.replace("'.$redirect_to.'")</script>';
    echo $redir;

    Hey Guys,

    I appreciate the time you guys have taken out and replied to my issue. Turns out that wasn’t the problem at all!

    My PHP form was submitting to itself so the page was reloading and then the page would lose the query string. so I added the query string logic to the form action so now it sticks after the form submits.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to use the redirect_to method properly on a custom login page?’ is closed to new replies.