1

How can I redirect users if they want to visit any other pages, but login page and password reset page?

My login page is actually my homepage and I want to redirect all of them to login page if they are not logged in. Also to allow them to reset password if they have forgotten them.

Thanks

1

You indicated that your login page is your home page. If you want all non-logged in users to then be directed to the home page to log in, you need to check a couple of things:

  1. Is the user logged in?
  2. Is the current page the home page?

You can do these things with is_user_logged_in() and is_front_page(). If these are not true, then you can use wp_redirect() to redirect the user to the home page.

Assuming your forgot password page is a "page," you could use is_page() in your logic as well.

You'll need to hook it to an action so that it fires late enough to allow checking the page, but early enough to still safely redirect the user (i.e. before headers are sent). template_redirect is ideal for this.

add_action( 'template_redirect', 'my_frontpage_redirect' );
function my_frontpage_redirect() {
    if ( ! is_user_logged_in() ) {
        if ( ! is_front_page() && ! is_page( 'my-password-reset' ) ) {
            wp_redirect( home_url() );
            exit();
        }
    }
}

Adjust the slug in the is_page() call according to whatever the page slug is for your password reset.

  • Butlerblog, man you did it! It works like a charm. Can you just please add that exception because reset password is actually on another page, as you guessed! Wow! – Vucko Aug 21 '17 at 21:50
  • Reset password page has url: domainexample.com/lostpassword, in case you need it. – Vucko Aug 21 '17 at 21:53
  • I edited the logic to also check that it's not 'my-password-reset'. You could change that to 'lostpassword' in your specific instance. – butlerblog Aug 21 '17 at 22:00
  • Works! Can you please share your contact with me, maybe I have some other questions and I will pay if needed. Thanks in advance! – Vucko Aug 21 '17 at 22:32
  • Glad to hear that worked for you. Contact info is in my profile. – butlerblog Aug 22 '17 at 16:11
0

You can add a rewrite rule into your .htaccess, checking for not-logged users, and redirect them to home page:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in.*$ [NC]
RewriteRule . /index.php [NC]

On your login page you should provide a link to reset forgotten password.

  • It says "Internal Server Error" and something about error 500. Thanks tho! – Vucko Aug 21 '17 at 20:36
  • did you put this AFTER standard WordPress entries in the htaccess? I mean, add the 4 lines at the end of your current .htaccess. Are you running WP as a subfolder of root (/) or not? In such a case you should edit row #2 and #4 aggordingly – Fabio Marzocca Aug 21 '17 at 20:38
  • I'm testing it locally. Putting it after WP entries, doesn't give error, but redirect still won't work + css files are not triggered. – Vucko Aug 21 '17 at 20:42
0

In Settings, Reading, enable the "Visible only to registered users of this site" button. All site visitors will see the login page.

After login, users will be directed to thier user profile page. You can change that redirection with code similar to this, which will redirect to the site's home page. (In your case, you'd need to change your home page to something else so they don't need to log in twice.)

function go_here_after_login( $redirect_to, $request, $user ) { 
    if (current_user_can( 'manage_options' )) {return $redirect_to;}
    else {return home_url();}

    return;
}
add_filter ('login_redirect', 'go_here_after_login', 10, 3);

There are lots of googles on how to change your login screen.

Note that you can use htaccess to require a login, but this method lets you use the WP User accounts you have on your site.

  • Thanks for answer, but I have already set with plugin different redirections for different users, depending on user roles. Will this overwrite it ? – Vucko Aug 21 '17 at 20:33
  • This may overwrite your plugin changes. Depends on the priority assigned by the plugin, and which filter they used. But you could easily use the above to change the 'destination' url to whichever you need. You could also modify the above to adjust the destination according to user 'group'. Which is probably what the plugin does - taking a look at the plugin code would give you more info. – Rick Hellewell Aug 21 '17 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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