A
A
alexiusgrey2021-11-27 19:06:44
WordPress
alexiusgrey, 2021-11-27 19:06:44

How to exclude child endpoint from myaccount redirect?

Users should not see the default login-registration form on the myaccount page. Must log in through the pop-up form, which is in the header of the site.
If you click on the icon of a person, then registered people will go to their account, and a pop-up form will open for unregistered people.

add_action('wp_logout','auto_redirect_after_logout');

function auto_redirect_after_logout(){

  wp_redirect( home_url() );
  exit();

}

I use a redirect to the home page when logging out of the account, but this does not exclude the user from getting to the default authorization form - if, for example, the person logs out of the account and then clicks "back" on the viewed page.
I tried to make a redirect from the account to all unregistered users.
add_action( 'template_redirect', 'wish_custom_redirect' );
function wish_custom_redirect() {
  global $wp;
  if (!is_user_logged_in() &&  is_page('my-account') ) {
    wp_redirect( '/' );
    exit;
  }
}

But the password recovery form also then redirects and does not open, it is a child of the account page, and the person will not be able to recover the password.
I tried like this
add_action( 'template_redirect', 'wish_custom_redirect' );
function wish_custom_redirect() {
  global $wp;
  if (!is_user_logged_in() &&  is_page('my-account') &&  !is_page('my-account/lost-password/')  ) {
    wp_redirect( '/' );
    exit;
  }
}

But it doesn't work, it still redirects. How to exclude it correctly, or what else is the way out?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2021-11-30
Semenov

You need to steer through is_user_logged_in

if ( is_user_logged_in() ) {
  echo 'Вы авторизованы на сайте!';
}
else {
  echo 'Вы всего лишь пользователь!';
}

this also comes in handy
wp_logout_url( $redirect );
$redirect(string)
URL to redirect to after exit
Default: $redirect is empty

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question