• Resolved fredy121

    (@fredy121)


    Hi,
    I have a custom function using admin-ajax.php to fetch the visitor geolocation details. This code automatically redirects the user to their region (if not in Australia/New Zealand) the first time and the second time a popup is displayed with the option to switch regions. This is working fine.
    However, if a visitor lands on a different language page (for example, a visitor from Italy Googled the website and landed on italian translated page), the visitor geolocation doesn’t work anymore. The admin-ajax.php fails to load response data.

    I didn’t find anything in google regarding this issue. Could someone help me with this?

    Thanks

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Edson Galina Fortes

    (@glx77)

    Hi @fredy121 ,

    hope you’re fine ? Sorry for the issue you’re facing. Do you have activate the autoswitch option from Weglot ? It may be a conflict with our features and yours. Can you check the log to have the details of the error when you run the function si_geolocate_visitor() ?

    Regards

    Thread Starter fredy121

    (@fredy121)

    Hi @glx77
    I am good, thanks. Hope you are doing good too.
    The autoswitch option in Weglot is not active and there are no error logs while running the function. On page load an ajax request is submitted which then returns the visitor geolocation details. This request is failing without any error details and I tried console logging the error. The only error visible is in developer tools console net::ERR_TOO_MANY_REDIRECTS

    Thanks

    Plugin Contributor Edson Galina Fortes

    (@glx77)

    Hi @fredy121 ,

    I’m good too thanks. That’s weird, if the autoswitch is not activate, weglot don’t make any redirection. I investigate and get you in touch. Are you able to track where the redirection come from ? Or can you send me the content of your si_geolocate_visitor() function, i’ll would like to test it in my side to find a solution

    Regards

    Thread Starter fredy121

    (@fredy121)

    Hi @glx77
    Unfortunately I couldn’t track where the redirection are coming from. These are my codes

    $.ajax( {
    	method: 'post',
    	url: si.ajaxUrl,
    	data: {
    		'action': 'si_geolocate_visitor'
    	},
    	dataType: 'json',
    	success: function( data ) {
    		if ( 'success' === data.status ) {
    			$window.trigger( 'si.visitor-geolocated', [ data.geolocation ] );
    		}
    	}
    } );
    
    function si_geolocate_visitor() {
    	header( 'Content-Type: application/json' );
    
    	$geolocation = get_visitor_geolocation(); // get visitor location from the location cookie data
    	if ( empty( $geolocation ) ) {
    		$geolocation = request_visitor_geolocation(); // if the cookie is empty, ie. for the first time visit request geolocation using ipstack service.
    		if ( ! empty( $geolocation ) ) {
    			set_details( 'geolocation', 'data', $geolocation ); // save geolocation in location cookie.
    		}
    	}
    
    	if ( ! empty( $geolocation ) ) {
    		wp_die( wp_json_encode( array(
    			'status'      => 'success',
    			'geolocation' => $geolocation
    		) ) );
    	}
    
    	wp_die( wp_json_encode( array(
    		'status' => 'error',
    	) ) );
    }

    I even tried removing the whole code from the si_geolocate_visitor function with just

    wp_die( wp_json_encode( array(
    	'status'      => 'success'
    ) ) );

    but this too didn’t return any response.

    Thread Starter fredy121

    (@fredy121)

    Hi @glx77
    Just checking on this. Were you able to test the code?
    Thanks.

    Plugin Contributor Edson Galina Fortes

    (@glx77)

    hi @fredy121 ,

    i’ve no test the code yet, I’ll test it asap (may be today or monday) and I’ll get you in touch

    Regards

    Plugin Contributor Edson Galina Fortes

    (@glx77)

    hi @fredy121 ,

    sorry for the late answer, hope you’re fine?
    Can you try this code and tell me if its works:
    Can you make your test ot on homepage ?

    $.ajax( {
    	method: 'post',
    	url: si.ajaxUrl,
    	data: {
    		'action': 'si_geolocate_visitor'
    	},
    	dataType: 'json',
    	success: function( data ) {
    		if ( 'success' === data.status ) {
    			$window.trigger( 'si.visitor-geolocated', [ data.geolocation ] );
    		}
    	}
    } );
    
    function si_geolocate_visitor() {
            add_filter( 'weglot_autoredirect_only_home', '__return_true' );
    	header( 'Content-Type: application/json' );
    
    	$geolocation = get_visitor_geolocation(); // get visitor location from the location cookie data
    	if ( empty( $geolocation ) ) {
    		$geolocation = request_visitor_geolocation(); // if the cookie is empty, ie. for the first time visit request geolocation using ipstack service.
    		if ( ! empty( $geolocation ) ) {
    			set_details( 'geolocation', 'data', $geolocation ); // save geolocation in location cookie.
    		}
    	}
    
    	if ( ! empty( $geolocation ) ) {
    		wp_die( wp_json_encode( array(
    			'status'      => 'success',
    			'geolocation' => $geolocation
    		) ) );
    	}
    
    	wp_die( wp_json_encode( array(
    		'status' => 'error',
    	) ) );
    }

    Regards

    Thread Starter fredy121

    (@fredy121)

    Hi @glx77 ,
    I am good, thanks. Hope you are fine too.
    I tried the code and there is no difference. Just a reminder that the autoswitch in weglot is disabled.

    To tested this further, I removed all the code from function si_geolocate_visitor() and replaced it with just a simple response

    add_filter( 'weglot_autoredirect_only_home', '__return_true' );
    wp_die( wp_json_encode( array(
    	'status' => 'success',
    ) ) );
    

    Still the response is the same.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Custom request to admin-ajax.php blocked’ is closed to new replies.