Support » Plugin: GeoIP Detection » AJAX, JS, & Cached Pages

  • Resolved TheRiz

    (@theriz)


    Hi Benjamin,

    I recently did some site speed optimizations, which included caching updates. I am now seeing some issues with the shortcode not displaying information correctly.

    I see there are BETA options for Ajax and JS.

    For the AJAX option, does this require any additional coding? I enabled this setting however it does not seemed to have helped.

    I will need to ask a developer help me with the JS setting if that is also required, but was not sure if just the AJAX setting adds any functionality on its own.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Benjamin Pick

    (@benjamin4)

    Currently AJAX needs JS coding, yes. It is planned that in the longterm if AJAX is enabled, the shortcodes are rendered client-side via AJAX but it will take some work until all shortcode parameters are supported. So maybe your developer could start working on this shortcode support with the parameters you need and contribute it to the plugin? I would be happy to assist, but I know I won’t be able to do it on my own in the next months.

    TheRiz

    (@theriz)

    We’re working through it, but our solution would likely need to be custom to our site. Caching has been a huge issue and we’re still trying to get this to fire correctly via ajax/JS. You can likely close this ticket for now, will check in again if anything is needed. thanks

    Any idea when this might be implemented? We are running into the same issue since a client site using your plugin is on Cloudways. I have to disable Varnish so it works normally.

    Also, will this require custom coding when it’s out of beta? Our use case for your plugin is on a 3 sites multisite network. One is for English Canada, one for Quebec (French) and one for he US. Unless a site visitor selects a language manually (we have links in a menu) I redirect them to the most appropriate site based on IP with a PHP function based on your plugin in my child theme.

    I’m far less comfortable with JavaScript so any pointers on reproducing the code below would be great.

    Thanks!

    add_action( 'template_redirect', 'zw3_build_user_redirect_url' );
    function zw3_build_user_redirect_url() {
    
    	$redirectURL = '';
    
    	if ( function_exists('geoip_detect2_get_info_from_current_ip' ) && !is_admin() && !is_user_logged_in() ) {
    
    		// Setup needed variables to determin where the user is and what site he's on
    		$userInfo = geoip_detect2_get_info_from_current_ip();
    		$userCountryCode = $userInfo->country->isoCode;
    		$userStateProvCode = $userInfo->subdivisions[0]->isoCode;
    		$userBrowserLang = substr( zw3_get_browser_language(), 0, 2 );
    		$currentSiteID = get_current_blog_id();
    		$currentSiteCountry = '';
    		$userSelectedSite = '';
    
    		//echo $userCountryCode;
    
    		// Check if user is on the correct site depending on 3 things :
    		// 1- They clicked a language link so are purposefully choosing a site
    		// 2- They clicked a language link in the last hour and a cookie was saved for that choice and exists
    		// 3- Neither of the above are true and the site choice is solely based on the user's location as determined by his IP IP
    
    		// First, if user has selected a site manually, save that in a cookie for one hour.
    		//  && !isset ( $_COOKIE[ 'user_selected_site' ] )
    		if( isset( $_GET[ 'noredirect' ] ) && sanitize_text_field( trim( $_GET[ 'noredirect' ] ) ) == 'en-CA' ) {
    
    			// User has selected the Canadian (root) site. Set the $userSelectedSite variable and user_selected_site cookie.
    			$userSelectedSite = 'CA';
    
    			if( !isset ( $_COOKIE[ 'user_selected_site' ] ) || $_COOKIE[ 'user_selected_site' ] != $userSelectedSite ) {
    
    				setcookie( 'user_selected_site', $userSelectedSite, time()+3600, COOKIEPATH, COOKIE_DOMAIN );
    
    			}
    
    			// Make sure $redirectURL is empty as we do not need to redirect the user since they just clicked the link to site they wanted.
    			$redirectURL = '';
    
    		} else if( isset( $_GET[ 'noredirect' ] ) && sanitize_text_field( trim( $_GET[ 'noredirect' ] ) ) == 'fr-CA' ) {
    
    			// User has selected the French Canadia (QC) site. Set the $userSelectedSite variable and user_selected_site cookie.
    			$userSelectedSite = 'QC';
    
    			if( !isset ( $_COOKIE[ 'user_selected_site' ] ) || $_COOKIE[ 'user_selected_site' ] != $userSelectedSite ) {
    
    				setcookie( 'user_selected_site', $userSelectedSite, time()+3600, COOKIEPATH, COOKIE_DOMAIN );
    
    			}
    
    			// Make sure $redirectURL is empty as we do not need to redirect the user since they just clicked the link to site they wanted.
    			$redirectURL = '';
    
    		} else if( isset( $_GET[ 'noredirect' ] ) && sanitize_text_field( trim( $_GET[ 'noredirect' ] ) ) == 'en-US' ) {
    
    			// User has selected the USA site. Set the $userSelectedSite variable and user_selected_site cookie.
    			$userSelectedSite = 'US';
    
    			if( !isset ( $_COOKIE[ 'user_selected_site' ] ) || $_COOKIE[ 'user_selected_site' ] != $userSelectedSite ) {
    
    				setcookie( 'user_selected_site', $userSelectedSite, time()+3600, COOKIEPATH, COOKIE_DOMAIN );
    
    			}
    
    			// Make sure $redirectURL is empty as we do not need to redirect the user since they just clicked the link to site they wanted.
    			$redirectURL = '';
    
    		} else if( isset ( $_COOKIE[ 'user_selected_site' ] ) && !isset( $_GET[ 'noredirect' ] ) ) {
    
    			if( !empty( $_COOKIE[ 'user_selected_site' ] ) ) {
    
    				$userSelectedSite = $_COOKIE[ 'user_selected_site' ];
    
    			}
    
    			// Make sure $redirectURL is empty as we do not need to redirect since the user has previously selected a site and the cookie is set.
    			$redirectURL = '';
    
    		}
    
    		// For users with no selection, set redirect URL based on user IP location
    		if( $currentSiteID == 1 && empty( $userSelectedSite ) ) {
    
    			// We're on the English Canadian site and user has no prior selection
    			$currentSiteCountry = 'CA';
    
    			if( $userCountryCode != $currentSiteCountry ) {
    
    				// We're on English Canadian site but user is in USA or outside Canada
    				// Redirect to US site
    				$redirectURL = '/us/';
    			}
    
    			if( $userCountryCode == $currentSiteCountry && $userStateProvCode == 'QC' ) {
    
    				// We're on English Canadian site but user is in Quebec
    				// Redirect to root Quebec site
    				$redirectURL = '/fr/';
    			}
    
    		} else if( $currentSiteID == 2 && empty( $userSelectedSite ) ) {
    
    			// We're on the Quebec site and user has no prior selection
    			$currentSiteCountry = 'CA';
    
    			if( $userCountryCode != $currentSiteCountry ) {
    
    				// We're on French Canadian (QC) site but user is in USA or somewhere else outside Canada
    
    				// Redirect to US site unless user's browser is localized in French
    				if( $userBrowserLang == 'fr' ) {
    					$redirectURL = '';
    				} else {
    					$redirectURL = '/us/';
    				}
    
    			}
    
    			if( $userCountryCode == $currentSiteCountry  && $userStateProvCode != 'QC' ) {
    
    				// We're on French Canadian (QC) site but user is in Canada but not in Quebec
    
    				// Redirect to CA site
    				$redirectURL = '/';
    			}
    
    		} else if( $currentSiteID == 3 && empty( $userSelectedSite ) ) {
    
    			// We're on the US site and user has no prior selection
    			$currentSiteCountry = 'US';
    
    			if( $userCountryCode != $currentSiteCountry && $userCountryCode == 'CA' && $userStateProvCode != 'QC' ) {
    
    				// We're on USA site but user is in Canada but not in Quebec
    				// Redirect to root (English Canadian) site
    				$redirectURL = '/';
    			}
    
    			if( $userCountryCode != $currentSiteCountry && $userCountryCode == 'CA' && $userStateProvCode == 'QC' ) {
    
    				// We're on USA site but user is in Canada but not in Quebec
    				// Redirect to root (English Canadian) site
    				$redirectURL = '/fr/';
    			}
    
    		}
    
    		//$userSelectedSite = 'User selected site: ' . $userSelectedSite . '<br>User Country Code: ' . $userCountryCode . '<br>User State/Prov Code: ' . $userStateProvCode . '<br>User Browser Lang: ' . $userBrowserLang . '<br>Redirect URL: ' . $redirectURL;
    		//print_r( $userSelectedSite );
    
    	}
    
    	if( !empty( $redirectURL ) ) {
    
    		wp_safe_redirect( $redirectURL );
    		exit;
    
    	}
    
    }
    Plugin Author Benjamin Pick

    (@benjaminpick)

    This will require custom coding, yes – mainly because I have no idea how a simple yet powerful way of configuring this would look like. Have you tried looking for other GeoIP plugins, there are some paid ones which have some redirect options …

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.