最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Need to redirect home page only on Woocommerce site if user logged in

programmeradmin3浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论