functions - Members only site - still need the lost password page accessible

admin2025-06-05  9

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

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

Share Improve this question edited Dec 7, 2018 at 17:39 Lyall asked Dec 7, 2018 at 15:39 LyallLyall 1351 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749119002a316523.html

最新回复(0)