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 ?
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 typeshttp://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:1. Modify your
.htaccess
file â add this line afterindex.php
rewrite rule:where
yoursecretpath
is your path to admin panel.2. Modify
login_header
function inwp-login.php
file â add this code just before line 59 (where HTML doc type tag starts, so to prevent headers sending error):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 🙂
If you will create a custom login page on your address
/login
, then create a rewrite: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:
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
andauthenticate
.