Skip Navigation

[Resolved] Conditional redirect to Homepage.

This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Himanshu Agarwal 5 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#1200799

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.

#1201007

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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

#1201213

Thank you Waqar, Now its working fine.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.