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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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' );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0