1

My site requires login before viewing any page and it works great:

function wpse_131562_redirect() {
if ( !is_user_logged_in() ) 
{auth_redirect();
}}
add_action('template_redirect', 'wpse_131562_redirect');

But obviously this means the link to the lost-password page just redirects back to the login page. I've tried changing it to:

function wpse_131562_redirect() {
if ( !is_user_logged_in() || (!ispage('lost-password') ))
{auth_redirect();
}}
add_action('template_redirect', 'wpse_131562_redirect');

But it has the same problem, and when I tried to use wp_lostpassword_url it broke completely.

How can I restrict access to everything other than the lost-password page?

Thanks

1 Answer 1

2

I believe your if statement is incorrect it should be an AND (&&) not OR

so try

if ( ! is_user_logged_in() && ! is_page( 'lost-password' ) ) {

EDIT

Try using $object = get_queried_object() for checking the post slug

$object = get_queried_object();
if ( ! is_user_logged_in() && ( ! $object || 'my-account' !== $object->post_name ) ) {
  // ...

EDIT 2 The page slug was wrong so updated in the second example

8
  • Thank you, I just tried that and I'm afraid it has the same problem. It redirects to domain.com/…
    – Lyall
    Commented Dec 7, 2018 at 16:02
  • @Lyall I have updated my answer with a different solution which uses get_queried_object instead, let me know if that works :)
    – jrmd
    Commented Dec 7, 2018 at 16:10
  • Close! The page loads but it has an error at the top of the page - Warning: Use of undefined constant is_user_logged_in - assumed 'is_user_logged_in' (this will throw an Error in a future version of PHP) in /homepages/45/d622530085/htdocs/xxxx/domain/wp-content/themes/exoplanet/functions.php on line 288
    – Lyall
    Commented Dec 7, 2018 at 16:15
  • (I will move this to the child theme functions later today)
    – Lyall
    Commented Dec 7, 2018 at 16:16
  • @Lyall that was meant to be a function call, my bad I'll update it
    – jrmd
    Commented Dec 7, 2018 at 16:21

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.