0

I added the following code via the cPanel to the function.php file.

    add_action('template_redirect','woo_check_loggedin');

function woo_check_loggedin(){
   // this condition checks for certian pages

      // if user is not logged in we redirect him to login page you can also show error message instead of redirect if that the requirment.
      if(!is_user_logged_in() && !is_page(lost-password) ){
          wp_redirect("https://example.com/wp-login.php");
          exit;
      }
      

   }

But, when I now go e.g. to mysite.com/shop it still works (I can visit every site). How can I avoid that? Because the site should only be avaiable for loggedin users. (But only the admin can create Logins).

7
  • is there a reason lost-password has no quotes? are you sure this isn't page caching or CDN caching?
    – Tom J Nowell
    Jan 22 at 1:25
  • The link: example.com/myaccount/lost-password needs to be reachable.
    – Luca Melop
    Jan 22 at 12:36
  • 1
    I understand, but your code has is_page(lost-password) and not is_page("lost-password"), have you tried adding the quotes? "". Are you sure that the function runs?
    – Tom J Nowell
    Jan 22 at 18:04
  • You probably want auth_redirect() rather than wp_redirect with a hard-coded URL.
    – Rup
    Jan 23 at 13:01
  • Which functions.php did you put this in? If it's a plugin are you sure that plugin is active? You could try adding a deliberate typo to the file and check that breaks the site temporarily.
    – Rup
    Jan 23 at 13:02

0

Your Answer

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

Browse other questions tagged or ask your own question.