18

I have a website, with a user system. I want to integrate wordpress's user system into that website's, but I still want to use the website's register/login pages. I don't want anybody to be able to login or register using Wordpress's login or registration forms. Instead, when they're try to access the login/registration pages in Wordpress, I want those pages to redirect them to my own login/registration pages.

Is there any way of doing this? I've tried Google, but all I could find was redirection AFTER the user logs in or registers, which is not what I want.

Thank you in advance.

Bruno De Barros
  • 1,535
  • 2
  • 16
  • 30

5 Answers5

27

For this you need to redirect login/registration page to your custom pages. So, Write this code in your functions.php file Under your activated theme folder. Pass your custom page path as a Argument.

 add_action('init','possibly_redirect');

function possibly_redirect(){
 global $pagenow;
 if( 'wp-login.php' == $pagenow ) {
  wp_redirect('http://google.com/');
  exit();
 }
}
adamj
  • 4,672
  • 5
  • 49
  • 60
nickohrn
  • 2,228
  • 15
  • 18
  • Where shall I register add_action call? where do I call this ? excuse me for I'm a .Net developer. – this. __curious_geek Jan 19 '10 at 10:43
  • You have two options. Either throw the above code in a plugin or create a functions.php in your themes' folder and add the above. – nickohrn Jan 19 '10 at 23:19
  • 8
    It should be added that doing this disables the ability to log-out unless explicitly done from the target landing page. – thinice Aug 22 '12 at 17:07
  • the same thing doing with the register page, not working for me: `if( 'wp-login.php?action=register' == $pagenow ) { wp_redirect('http://google.com/'); exit(); }` Not working for me. – Mayeenul Islam Aug 04 '13 at 20:27
  • 2
    Mayeenul, you need to just test 'wp-login.php' == $pagenow and if you need to specifically detect the registration action, add a check like (isset($_GET['action']) && 'register' === $_GET['action']) – nickohrn Aug 05 '13 at 00:08
  • This is good, but if the custom login page is using wp_login_form() then you need to also check to make sure this request isn't being posted, otherwise all login attempts will redirect back to the custom login form (without logging in). Something like if( 'wp-login.php' == $pagenow && $_SERVER['REQUEST_METHOD'] == 'GET') – Nathan Pond Nov 15 '16 at 03:39
20

To restrict direct access only for 'wp-login.php', without POST or GET request (useful for custom ajax login forms), I use the advanced function:

function possibly_redirect(){
  global $pagenow;
  if( 'wp-login.php' == $pagenow ) {
    if ( isset( $_POST['wp-submit'] ) ||   // in case of LOGIN
      ( isset($_GET['action']) && $_GET['action']=='logout') ||   // in case of LOGOUT
      ( isset($_GET['checkemail']) && $_GET['checkemail']=='confirm') ||   // in case of LOST PASSWORD
      ( isset($_GET['checkemail']) && $_GET['checkemail']=='registered') ) return;    // in case of REGISTER
    else wp_redirect( home_url() ); // or wp_redirect(home_url('/login'));
    exit();
  }
}
add_action('init','possibly_redirect');
Anatoly
  • 201
  • 2
  • 2
  • The accepted answer doesn't work for those of us using wp_login_form() - use this one instead. – piersb Apr 21 '17 at 14:01
5

The correct action hook is login_init that only fires on wp-login.php.

Here, no ?action=action-name is being requested, so it's the main login page:

add_action('login_init', function(){
    if( !isset( $_GET['action'] ) ) {
        wp_redirect( 'http://example.com' );
    }
});

For all requests, we can use a specific hook login_form_ACTION-NAME, ie, postpass, logout, lostpassword, retrievepassword, resetpass, register and login. Example:

add_action('login_form_register', function(){
    wp_redirect( site_url('custom-registration/') );
});
brasofilo
  • 25,496
  • 15
  • 91
  • 179
2

If you're making use of a custom login page, but still using wp_login_form(), be aware that the form will POST to wp-login.php, so you will want to check if $_POST is empty before redirecting.

function prefix_wp_login_redirect() {
  global $pagenow;  
  if( $pagenow == 'wp-login.php' && empty($_POST)) {
    auth_redirect();
    exit();
  }
}
cee.e
  • 159
  • 1
  • 1
  • 9
1

You might be able to latch onto the login_head hook and issue a redirect there.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368