John Davidson

php - Woocommerce how to exclude the child pages (endpoints) of myaccount from the template redirect hook?

0 comments
Message:


The login-register form has to be shown only like popup, so I've made redirect, to avoid default myaccount page for not logged 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;
}
}

To view their account page users have to log in or register in popup form.
But there is a problem - /my-account/lost-password/, my-account/reset-password/ are children-endpoints of myaccount. They have not to make redirect for non-logged users.
I tried to make like that



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 still redirects. Maybe it's a bad solution at all and there's a better way? Or how to make this redirect correctly?


add_action('wp_logout','auto_redirect_after_logout');

function auto_redirect_after_logout(){

wp_redirect( home_url() );
exit();
}

To redirect only on logout helps, but doesn't avoid user to see the default page. They can logout, and then return on the prevous page /myaccount, and see that default register form.



My Answer:
&& is_page('myaccount')) {
wp_redirect(home_url());
exit;
}
}

This code snippet will redirect non-logged in users away from the "myaccount" page and its child pages (endpoints) to the home page. This way, the login-register form will only be shown as a popup and not on the default "myaccount" page.

Rate this post

4 of 5 based on 3453 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us