3

I would like to redirect this page wp-login.php to /login page. So when a user clicks on a link like this <a href="/wp-login.php">login</a>, wordpress will redirect him to /register .

I found this function and modified it :

add_action('init','custom_login');

function custom_login(){
 global $pagenow;
 if (( 'wp-login.php' == $pagenow ) && (!is_user_logged_in())) {
  wp_redirect('/login');
  exit();
 }

I am not good with wordpress coding.

The redirection works fine, but the authentication seems to be broken. When I enter my login/pass, the same page (/login) reloads without being authenticated.

Any suggestion to fix the function for redirection ?

1
  • what you want is a custom login page, you can find numerous tutorial on how to do it, and about stopping the redirection of custom login page to wp-login.php the following Link might help. Commented Jan 7, 2014 at 11:17

4 Answers 4

3

If you're only concerned about login links displayed on your pages, you should be able to modify the URL by hooking the login_url filter. This won't redirect a user that types http://MYSITE/wp-login.php directly into their browser, but it should affect the login links displayed throughout your site.

This is the example code from the login_url Codex page:

add_filter( 'login_url', 'my_login_page', 10, 2 );
function my_login_page( $login_url, $redirect ) {
  return home_url( '/my-login-page/?redirect_to=' . $redirect );
}
0

1. Modify your .htaccess file – add this line after index.php rewrite rule:

RewriteRule ^index\.php$ – [L]<br />
RewriteRule ^yoursecretpath$ wp-login.php [L,NC,QSA]

where yoursecretpath is your path to admin panel.

2. Modify login_header function in wp-login.php file – add this code just before line 59 (where HTML doc type tag starts, so to prevent headers sending error):

<?php $uri = $_SERVER['REQUEST_URI']; if(stripos($uri, ‘wp-login’) or stripos($uri, ‘wp-admin’) && !stripos($GLOBALS["HTTP_COOKIE"], ‘wordpress_logged_in’)) {  header("Location: ".get_site_url().'/yoursecretpath' ); exit(); } ?>
0

I actually already have an open source plugin that does this already:

https://github.com/tripflex/wp-login-flow

It was created to require users to verify their emails but you can disable that feature and just use the rewrite feature which is functional in the latest version on WordPress repo (1.0.0).

You can look at the code to see how I handle the rewrites:

https://github.com/tripflex/wp-login-flow/blob/master/classes/rewrite.php#L312

I'm actually working on this now to have some major updates and patches, but for now 1.0.0 works perfectly fine for rewrites. Enjoy :)

-2

If you will create a custom login page on your address /login, then create a rewrite:

function restrict_admin() {

    if ( ! current_user_can( 'manage_options' ) && '/wp-admin/admin-ajax.php' != $_SERVER['PHP_SELF'] ) {
                wp_redirect( site_url() );
    }
}
add_action( 'admin_init', 'restrict_admin', 1 );

This code will check if the user is an administrator and if they are not WordPress will redirect them back to the website home page.

Create also a side template for your custom login and use the login defaults of WordPress inside the template:

$args = array( 'redirect' => site_url( $_SERVER['REQUEST_URI'] ) );
wp_login_form( $args );

You can also change the redirect adress for the login page. But is also useful, that you check on login errors and maybe you add code for right redirect after failed login. For this jobs use the hook wp_login_failed and authenticate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.