This question is solved here-
https://toolset.com/forums/topic/conditional-redirect-to-homepage/
But after this solution, if user is logged out then all singular product is redirecting on home page.
I need to redirect it on a condition from custom field like this
[wpv-conditional evaluate="false" if="('[wpv-current-user info='id']' ne ' ')"]
[wpv-conditional if="( $(wpcf-only-login-user) ne ' ' )"]
[direct_to_home]
[/wpv-conditional]
[/wpv-conditional]
And my code in function.php is
function make_redirect_to_home() {
do_action('template_redirect');
}
add_shortcode( 'direct_to_home', 'make_redirect_to_home');
function tssupp_redirect() {
if ( !is_user_logged_in() && is_singular('product') ) {
wp_redirect(home_url());
exit;
}
}
add_action('template_redirect', 'tssupp_redirect');
Please help me to solve this.
Hi Himanshu,
Thank you for contacting us.
Nigel will be away until Tuesday, but I'll be happy to assist you with your question.
As Nigel mentioned in his message, the "wp_redirect" needs to be called before the page's rendering/output has started. This is why using it with "wpv-conditional" shortcode won't work.
To include a check for the value of a custom field "only-login-user", you can update the code from:
function tssupp_redirect() {
if ( !is_user_logged_in() && is_singular('product') ) {
wp_redirect(home_url());
exit;
}
}
add_action('template_redirect', 'tssupp_redirect');
To:
function tssupp_redirect() {
if ( !is_user_logged_in() && is_singular('product') ) {
// get the custom field value
$field_check = types_render_field( "only-login-user", array( ) );
// redirect only if the value for the custom field "only-login-user" is not empty
if(!empty($field_check)) {
wp_redirect(home_url());
exit;
}
}
}
add_action('template_redirect', 'tssupp_redirect');
The above code will make sure that the redirection only happens for those products, for which the value for the custom field "only-login-user" exists or is not empty.
I hope this helps.
regards,
Waqar
Thank you Waqar, Now its working fine.