Redirecting users to a custom login page while using WordPress

Posted on

So your running a community powered website using WordPress, maybe you have BuddyPress installed as well. But you don’t like the login page and want to add your own. This snippet will get you part of the way there. You could modify this for other WP admin pages as well, but you would probably want to do some user role checking. Alternatively you could just hire someone like me to do your custom WordPress development.

For the DIY crowd, just place this in your functions.php and change out the $login_page to your custom login page (example goes to homepage) and any visitors will go to your custom login page.


function redirect_login_page(){

 // Store for checking if this page equals wp-login.php
 $page_viewed = basename($_SERVER['REQUEST_URI']);

 // Where we want them to go
 $login_page  = site_url();

 // Two things happen here, we make sure we are on the login page
 // and we also make sure that the request isn't coming from a form
 // this ensures that our scripts & users can still log in and out.
 if( $page_viewed == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET') {

  // And away they go...
  wp_redirect($login_page);
  exit();

 }
}

add_action('init','redirect_login_page');
  • Pingback: Redirecting users to a custom login page while using WordPress … | Scion Players

  • http://twitter.com/paradisewebtx Paradise Web Design

    been using this for a little while, but one of the shortest code versions

  • production051

    This is wicked. Its exactly what I was looking for & I’d like to know more.

    I’ve got the custom registration page which works fine but what I want to do is redirect logged in users to the accounts page/home page if they try and access the registration page by the url or a link.

    I used your code to redirect the default login & register page to my custom pages for it. I just think that users shouldn’t be able to access the registration page if they are already logged in.

  • Pingback: How to Build a Fully Customized WordPress Login Page « theGlassOnion

  • Pingback: How to Build a Fully Customized WordPress Login Pagefuturetechnologygadgets.com | futuretechnologygadgets.com

  • Pingback: How to Build a Fully Customized WordPress Login Page | Free Theme Premium WordPress Joomla

  • Pingback: How to Build a Fully Customized WordPress Login Page

  • Pingback: How To Build A Fully Customized WordPress Login Page Zem Time

  • Petit Aah

    Hi!

    What’s up? My name is Seb.
    I’m a beginer and I tried your code, but didn’nt work out for me. ( cause I did a mistake but don’t know which…)
    I need to redirect any wp-login.php command of my theme to a page like http://mydomain.com/?page_id=424
    How would you put this in the script?

    And…

    I also need to redirect the register page (wp-login.php?action=register) to a page like http://mydomain.com/?page_id=424

    How would you do that?
    If you know… Thank you very much in advance!
    See you Seb:
    my mail: spetitat@hotmail.com
    Thank you for your help!

  • CaroleP

    Hi, if we replace $login_page by, per example, my exact own personalized Inscription page url which is “espace-membre/inscription-2/” it will certainly not work !, has we mix html with PHP – Something has to be precised into that explanation… (For those not expert in PHP at least).
    Please help.

  • Pingback: How To Build A Fully Customized WordPress Login Page | vienergie

  • Bevels

    For anyone wanting to know how to enter a custom URL.

    Simply change:

    $login_page = site_url();to
    $login_page = site_url(“/login-page”);

    Where /login-page is the name of the page you’d like to redirect your visitors – For the sake of absolute clarification… http://www.yoursite.com/login-page

    Thanks very much for a simple and effective script.

    B

  • yonox

    working perfectly

  • Pingback: How to Build a Fully Customized WordPress Login Page | DailyMashable

  • http://wpuidev.com/blog Ali Sajjad

    But how this works for the eCommerce Platform?

  • Marek

    Hi there, it works, but only when I visit wp-login.php, when I open wp-admin which gets me wp-login.php?redirect=….. then it doesnt work. Can you help?

    • Montana Flynn

      Hi Marek, the problem is the if statement is checking for the exact match of wp-login.php, so let’s do something to make the $page_viewed variable only contain the first 12 characters so it’ll match all urls that begin with wp-login.php (which is 12 characters long). Here’s the change on line 4, I didn’t test it but I think it should work!

      $page_viewed = substr(basename($_SERVER[‘REQUEST_URI’]), 0, 12);

      • PAN

        how is the redirection solved that Marek stated?

    • http://www.wpexplorer.com AJ

      Just use false !== strpos( $page_viewed, ‘wp-login.php’ )

      • Jason Robie

        where does that go, AJ??

  • whatareyounew

    What about if a user types a wrong password or username? Right now it redirects to the wp-login.php page

  • isaacalves

    if you use /wp-admin it doesn’t work (redirects to wp-login.php with query strings)