I'm pulling my hair out with this one. I've tried 4 or 5 different approaches but none work consistently.
We have a WP site that includes a blog and Woocommerce for wholesale customers. We have a retail site at Shopify. We now need to redirect just the home page to if the user IS NOT logged in, and all other pages work as usual (e.g. /).
I'm using the Redirection plugin and have tried this regex rule:
^\/(?!wc-ajax)$
The 'wc-ajax' bit is for ajax requests that WC makes to the root url so we need to exclude those.
This rule seemed to work at first but not consistently. It also would redirect for everyone, including wholesale customers, which I don't want.
I also tried this code in a custom plugin:
add_action( 'init', 'my_redirect_home_page_only' );
function my_redirect_home_page_only() {
if ( !is_user_logged_in() && is_front_page() ) {
wp_redirect( '' );
exit;
}
}
Couldn't get that to work either. I tried 'wp' and 'template_redirect' instead of the init hook as well.
Any ideas?