admin管理员组

文章数量:1330609

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' );

本文标签: plugin developmentRedirection from a specific page for users logged in but not with membership