如果未登录,则重定向到注册页面并尝试访问受限页面 [英] Redirect to register page if not logged in and try to access restricted page

查看:35
本文介绍了如果未登录,则重定向到注册页面并尝试访问受限页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想限制对我的 woocommerce 网站上的产品的访问.如果用户未登录,我想将他们重定向到注册页面.我正在使用此代码,但我所有的产品都重定向到注册页面:

I would like to restrict the access to a product on my woocommerce website. If user are not login, I want to redirect them to register page. I'm using this code but all my products redirect to register page :

function wpse_131562_redirect() {
    if (! is_user_logged_in()

&& (is_woocommerce() || is_cart() || is_single(1735) || is_checkout())
) {

   // feel free to customize the following line to suit your needs

   wp_redirect( 'https://www.la-chaine-maconnique.fr/my-account/' );

   exit;
   }
}
 add_action('template_redirect', 'wpse_131562_redirect');

你想到了什么?

推荐答案

您可以尝试验证操作符是否正常工作!您可以更改&&"使用and",或者只在 if 语句中放置一个条件:这意味着执行以下操作:(请注意,这将重定向所有用户,甚至是已登录的用户)

You can try to verify the operator is working properly! You can change "&&" with "and", or just put one condition in the if statement: which means do the following: (note that this will redirect all users, even the signed-in ones)

function wpse_131562_redirect() {
   if ( is_single(1735)) {

   // feel free t`enter code here`o customize the following line to suit your needs
   wp_redirect( 'https://www.la-chaine-maconnique.fr/my-account/' );
   exit;
   }
}
add_action('template_redirect', 'wpse_131562_redirect');

如果上述代码仅适用于您定位的单个产品,请尝试您的操作.更好的解决方案是避免使用 is_signle(),据报道 is_singleWooCommerce 无法正常工作,您可以尝试以下操作:

If the above code works for only the single product you target, then experiment with your operates. A better solution is that you avoid is_signle(), it's reported that is_single doesn't work properly with WooCommerce, you can try the following:

function wpse_131562_redirect() {
    global $post;
    if ($post->ID = '1735' and !is_user_logged_in()) {
        // whatever you try to do here for product id = 123 will work!
        wp_redirect( 'https://www.la-chaine-maconnique.fr/my-account/' );
    }
}
add_action('template_redirect', 'wpse_131562_redirect');

这篇关于如果未登录,则重定向到注册页面并尝试访问受限页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆