0

I have this code in a template file that will contain my vue app that I want to load in WordPress.

<?php 
/*
* Template name: App
*/
ob_start();
get_header();

if( is_user_logged_in() ) {
?>
    <div id="app"></div>
<?php
} else {
    wp_redirect( wp_login_url() );
    exit;
}
get_footer();
ob_get_flush();
?>

I want to redirect users to the login page and this seems working fine, what I need to do is to rename the wp-login.php page in login.php or login and I've tried using .htaccess rewrite rule

# BEGIN WordPress
# Le direttive (linee) tra `BEGIN WordPress` e `END WordPress` sono
# generate dinamicamente, e dovrebbero essere modificate solo tramite i filtri di WordPress.
# Ogni modifica alle direttive tra questi marcatori verrà sovrascritta.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /mysite/extranet/
# Riscrittura url di login
RewriteRule ^login$ /wp-login.php [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mysite/extranet/index.php [L]
</IfModule>

# END WordPress

I've noticed that this will not work as expected. Is there something else I can do to achive this?

5
  • 1
    Wouldn't a if( ! is_user_logged_in() ) { be simpler? It would avoid the need for output buffers. I'd also suggest using wp_safe_redirect since you already know that you're redirecting to a URL on the same install
    – Tom J Nowell
    Commented Aug 7, 2023 at 11:33
  • Looking into this, the approach of using htaccess, or trying to change wp-login.php will never work, but creating your own login page at the URL you want, then using filters to force WP and wp_login_url to return your custom URL instead is both widespread and a question that's already been asked and answered on this stack in many forms. wordpress.stackexchange.com/search?q=custom+login+page
    – Tom J Nowell
    Commented Aug 7, 2023 at 12:19
  • Creating custom login page will imply that I need to create all the needed form fields. I've tried in past uing the wp_login_form() but It will come unstyled and it's not what I want.
    – OHICT
    Commented Aug 7, 2023 at 15:59
  • 1
    that function isn't the only way to do it, but yes you need to style things, that is unavoidable. wp-login.php cannot be moved or rewritten, not without forking WordPress and making major structural changes, that's why there are filters such as login_url
    – Tom J Nowell
    Commented Aug 8, 2023 at 10:39
  • 1
    also wp_login_form is not necessary, it's just a helper function used by the login/logout widget, not the singular official method of printing a login form. Lots of people build login forms without ever touching that function
    – Tom J Nowell
    Commented Aug 8, 2023 at 10:47

1 Answer 1

1

what I need to do is to rename the wp-login.php page in login.php or login and I've tried using .htaccess rewrite rule

Is there something else I can do to achive this?

No, wp-login.php cannot be moved or rewritten, not without modifying WordPress core. That particular approach to this is a dead end.

But the login URL can be swapped/replaced with something that is not wp-login.php.

The commonly accepted approach to this problem is to instead use the login_url filter to change where WordPress thinks the login URL is and point it to a custom login page of your own creation. How that page is constructed is up to you, e.g. you could use wp_login_form(), or construct the form yourself, or use widgets/blocks/plugins/form plugins/etc to do it. There are many ways to do this.

This does mean that you'll need to build that custom login page, and style it, and depending on how you build it will determine how much effort is required. How you do that would make a good question on this stack, but there are already lots of questions, and even entire tags dedicated to this subject.

If you just want to see the standard login page that you get out of the box, but at a different URL, then no, WordPress provides no way to do that via configuration/code/filters.

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.