plugin development - Redirection from a specific page for users logged in but not with membership

I am trying to redirect logged in user but not with a membership, from a specific page to product page. But it is showin

I am trying to redirect logged in user but not with a membership, from a specific page to product page. But it is showing error if I land on that page. Let's say join is the page with id 1867. Here is my code:

function redirect_member_to_product(){
    if (is_page(1867)){
        if (is_user_logged_in() && !wc_memberships_is_user_active_member('channel-mcgilchrist')){
            template_redirect('/product/channel-mcgilchrist');
            exit;
        }
    }
}
add_action('template_redirect', 'redirect_member_to_product');

What I am doing wrong here?

I am trying to redirect logged in user but not with a membership, from a specific page to product page. But it is showing error if I land on that page. Let's say join is the page with id 1867. Here is my code:

function redirect_member_to_product(){
    if (is_page(1867)){
        if (is_user_logged_in() && !wc_memberships_is_user_active_member('channel-mcgilchrist')){
            template_redirect('/product/channel-mcgilchrist');
            exit;
        }
    }
}
add_action('template_redirect', 'redirect_member_to_product');

What I am doing wrong here?

Share Improve this question edited Jul 13, 2020 at 11:14 Sally CJ 40.1k2 gold badges28 silver badges49 bronze badges asked Jul 13, 2020 at 6:06 Nayan ChowdhuryNayan Chowdhury 33 bronze badges 1
  • So I moved my previous comment to an answer and also, I rolled back your edit because you shouldn't edit your code like that because it invalidated the original question. :) – Sally CJ Commented Jul 13, 2020 at 12:10
Add a comment  | 

1 Answer 1

Reset to default 0

Your code is fine to me, except you made a mistake here:

template_redirect('/product/channel-mcgilchrist');

WordPress does not have a function named template_redirect, only a hook with that name.

So you should have used either wp_redirect() or wp_safe_redirect() which are valid functions to perform URL/page redirections in WordPress.

wp_safe_redirect( '/product/channel-mcgilchrist' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742266719a4411932.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信