Skip Navigation

[Resolved] Conditional redirect to Homepage.

This support ticket is created 5 years, 9 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

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

Assisted by: Nigel.

Author
Posts
#1199164

Hi,

I need to redirect user to home page on condition like this

[wpv-conditional evaluate="false" if="('[wpv-current-user info='id']' ne  '')"]

I tried with below code:-

[wpv-conditional evaluate="false" if="('[wpv-current-user info='id']' ne  '')"]
[direct_to_home]
 [/wpv-conditional]

and code in function.php

function make_redirect_to_home() {
     do_action('redirect_home_condi');
}
add_shortcode( 'direct_to_home', 'make_redirect_to_home');
 
add_action('redirect_home_condi', 'func_to_redirect');
function func_to_redirect() {
	
	if ( is_user_logged_in() ) 
	{ } else {
		wp_redirect( 'home_page_url');
        exit(); 
		}     
}

But this giving me this error "redirected you too many times", or "not redirect properly".

I also tried with this :-

ob_clean();
$url = get_home_url() ;
wp_redirect($url);
exit();

In else condition.

Please help to solve this, and how to make a conditional redirect?

#1199230

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You should redirect before rendering the page begins, so a custom shortcode on the page is too late.

You need a more direct approach, like this:

function tssupp_redirect() {

	if ( !is_user_logged_in() && is_page('inaccessible') ) {
		wp_redirect(home_url());
		exit;
	}

}
add_action('template_redirect', 'tssupp_redirect');

Here I am preventing guest users from seeing a page with a slug of 'inaccessible', but you can pass a page ID (or an array of page IDs or page slugs). That's for static pages. For other URLs you might need a different conditional tag to identify the current page: https://developer.wordpress.org/themes/basics/conditional-tags/

#1199700

Thank you Nigel,

I made the changes as you suggested. I need it for a woocommerce product, so i create the code like this:-

add_action('redirect_home_condi', 'func_to_redirect');
function func_to_redirect() {
	
	if ( !is_user_logged_in() && is_product() ) {
		echo "Redirect to Home.";
        wp_redirect(home_url());
        exit();
    }    
}

But its still not working, to check code i echo something and "Redirect to Home." is showing but redirection in not working.
Please help to correct this.

#1199745

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You haven't changed your code to match mine. You need to use the template_redirect hook.

If you want this for single product pages you can use the conditional tag is_singular (https://developer.wordpress.org/reference/functions/is_singular/).

function tssupp_redirect() {
 
    if ( !is_user_logged_in() && is_singular('product') ) {
        wp_redirect(home_url());
        exit;
    }
 
}
add_action('template_redirect', 'tssupp_redirect');
#1199879

Thank you Nigel, now its working fine.