Woocommerce Restrict My Account Page but NOT Lost Password Page

The name of the picture


Woocommerce Restrict My Account Page but NOT Lost Password Page



I have a WordPress site with Woocommerce. In Woocommerce, Lost Password URL merges with My Account page (www.site.com/my-account/lost-password/).


(www.site.com/my-account/lost-password/)



Then I create a redirect function that will redirect non-logged in users when they hit "My Account" page to "Login" page. This means I limit them too to access the Lost Password page.


add_action( 'wp', 'redirect' );
function redirect() {
if ( is_page(array('my-account', 'payment-screen', 'submit-resume', 'post-a-job')) && !is_user_logged_in() ) {
wp_redirect( home_url('/login') );
die();
} elseif ( is_page(array( 'register', 'login' )) && is_user_logged_in() ) {
wp_redirect( home_url('/my-account') );
die();
}
}



I want to restrict non-logged in users only for www.site.com/my-account/ and redirect them to www.site.com/login/ but NOT www.site.com/my-account/lost-password/. How can I do that?


www.site.com/my-account/


www.site.com/login/


www.site.com/my-account/lost-password/





On your lost-password page set parent to no-parent
– Saad Suri
Mar 5 at 7:55




lost-password


no-parent





@SaadSuri Where I can find lost-password page? I don't see that page in my site, I guess Woocommerce directly provide them in templates file.
– Anggriyawan Nugraha Prasetya
Mar 5 at 8:01




1 Answer
1



I resolve on this way:


add_action( 'template_redirect', 'class_to_login_page', 20 );
function class_to_login_page(){
global $post;
global $wp;
$request = explode( '/', $wp->request );
$page_id = get_option('woocommerce_myaccount_page_id');
if(!is_user_logged_in() && $post->ID == (icl_object_id($page_id, 'page')) && end($request) !== 'lost-password'){
wp_redirect( home_url('/login') );
exit;
}
}



I check if user is logged, if the ID is equal for my-account page and if is not the lost-password endpoint.
I hope this help you ;)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS