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');