使WordPress页面仅供WooCommerce登录用户访问

时间:2016-02-17 作者:kunato

我已经在WordPress上创建了一个页面,我想设置只有woocommerce用户才能查看它。这意味着他们必须先登录才能查看它。对于未注册的用户,如果他们试图访问该页面,我们会将他们重定向到我的帐户页面。

我找到了类似的解决方案,但这只适用于woocommerce页面,但如何插入wordpress页面的代码。代码如下:

function wpse_131562_redirect() {
    if (
        ! is_user_logged_in()
        && (is_woocommerce() || is_cart() || is_checkout())
    ) {
        // feel free to customize the following line to suit your needs
        wp_redirect(site_url(\'my-account/\'));
        exit;
    }
}

add_action(\'template_redirect\', \'wpse_131562_redirect\');
假设我的页面是mydomainname.com/wordpress-pages

如何限制该页面?期待您的帮助。

1 个回复
最合适的回答,由SO网友:Sumit 整理而成

在if条件中添加页面slug。

function wpse_131562_redirect() {
    if (! is_user_logged_in()
        && (is_woocommerce() || is_cart() || is_checkout() || is_page(\'wordpress-pages\'))
    ) {
        // feel free to customize the following line to suit your needs
        wp_redirect(site_url(\'my-account/\'));
        exit;
    }
}

add_action(\'template_redirect\', \'wpse_131562_redirect\');