• Jajabingz

    (@ferryavb)


    Hi Benjamin,

    I am trying to implement this feature “Redirect depending on country” mentioned in the following page.

    What I did was as follows:
    1) Create a custompaget1.php script as shown below.
    2) This script would reside in my public_html/wp-content/themes/directory2-child
    3) Create a new page in wordpress using the above script

    However, the redirecting does not seem to work. I get a blank page when I load up the page. My aim is to create redirects for particular pages within wordpress.

    Thank you.

    <?php /* Template Name: CustomPageT1 */ ?>
    
    <?php
    
    add_action('template_redirect', 'geoip_redirect', 5);
    function geoip_redirect(){
    	if (is_admin())
    		return;
    
    	// This condition prevents a redirect loop:
    	// Redirect only if the home page is called. Change this condition to the specific page or URL you need.
    	if (!is_home())
    		return;
    
    	if (!function_exists('geoip_detect2_get_info_from_current_ip'))
    		return;
    
    	$userInfo = geoip_detect2_get_info_from_current_ip();
    	$countryCode = $userInfo->country->isoCode;
    	switch ($countryCode) {
    		case 'DE':
    			$url = '/germany';
    			break;
    		case 'US':
    			$url = '/usa';
    			break;
    		case 'SG':
    			$url = 'www.google.com';
    			break;
    		default:
    			$url = 'www.google.com';
    	}
    	if ($url) {
    		wp_redirect(get_site_url(null, $url));
    		exit;
    	}
    }
    ?>
Viewing 1 replies (of 1 total)
  • Plugin Author Benjamin Pick

    (@benjamin4)

    Hm, interesting.

    I’m not sure if this works combined with custom templates, because the
    action ‘template_redirect’ is already done when the custom templates are
    loaded.

    remove this part:
    // This condition prevents a redirect loop:
    // Redirect only if the home page is called. Change this condition to
    the specific page or URL you need.
    if (!is_home())
    return;

    Check if the function is executed by adding an echo.
    If not, remove the function wrapping.

    Also, wp_redirect may require that nothing is outputed to the page yet, so remove the space between the ?> and <?php

    These are some hints to get you started, but I can’t give you more support than that.

Viewing 1 replies (of 1 total)
  • The topic ‘Implementing redirect feature on individual wordpress page’ is closed to new replies.