How to show Joinchat only in certain countries

Introduction

There are times when a company only offers its services or products in a single territory, so it does not make sense to show the Joinchat floating button outside that location. Let’s see how we can define in which country or countries Joinchat is shown, hiding this contact channel from the rest.

No esIt is not the first time someone has asked us about this, but it is the first time they ask us for a solution to implement. A couple of days ago we received the request from Repscan, a company from Barcelona dedicated to improving the online reputation of companies and individuals, they delete and generate content legally and permanently.

His message was this:
“So far it’s working great for us and it helps us a lot in Leads conversion but we have a problem with people coming from LATAM because we can’t offer the service.
Would there be any way to show the chat icon only to people from Spain or limit it to certain countries.”

To implement this functionality we are going to use a “Snippet” and as in other occasions we will use the plugin Code Snippets, and from here you can download the JSON to be able to import it from this plugin, but if you feel comfortable you can do it as you prefer.

Code

function joinchat_hide_by_country( $show ) {

	// true for included countries, false for excluded countries.
	$is_whitelist = true;

	// List of countries.
    // Edit list to your needs, e.g. for Iberian Peninsula (Spain & Portugal)
	$countries = array(
		'ES',
		'PT',
	);

	// Keep privacy anonymize user ip.
	$user_ip = wp_privacy_anonymize_ip( $_SERVER['REMOTE_ADDR'] );
	$cached_ip = 'ipcountry_' . md5( $user_ip );

	if ( ! $country = get_transient( $cached_ip ) ) {

		// Retreive two-letter country code (ISO 3166-1 alpha-2).
		// For more options view API doc (https://ip-api.com/docs).
		$response = wp_remote_get( 'http://ip-api.com/json/' . $user_ip . '?fields=status,countryCode' );
		$data = json_decode( wp_remote_retrieve_body( $response ) ?: '{"status":"error"}' );

		if ( $data && $data->status === 'success' ) {
			$country = $data->countryCode;

			// Cache country in transient (1 day) for faster future responses
			// and don't pass rate limit (45 requests per minute from an IP address).
			set_transient( $cached_ip, $country, DAY_IN_SECONDS );
		}
	}

	if ( ! $country ) {
		return $show;
	}

	// Check current country.
	if ( $is_whitelist && ! in_array( $country, $countries ) || ! $is_whitelist && in_array( $country, $countries ) ) {
		return false;
	}

	return $show;

}
add_filter( 'joinchat_show', 'joinchat_hide_by_country' );

As you can see it is perfectly commented and it is very simple, you only have to add the countries in which you want Joinchat to be shown, in the example we give you it would be the Iberian Peninsula (‘ES’ and ‘PT’), so that only visitors from these countries will see the Joinchat floating button to contact.

To make this possible we will use the Gelolocation IP-API. EIt is free with a limit of 45 requests per minute from the same IP. To avoid repeating API calls querying the same user IP and to have a faster response, the result is cached in a “transient” for 24 hours.

Another important point is that the user’s IP is anonymized to maintain privacy and not have any GDPR issues 😉.

Country codes should use the two-letter format (ISO 3166-1 alfa-2).

NOTE

This query is performed on the server with each request so it will not work with page caching.

Content

Versión gratuita

Snippets

Versión Premium

Chat Funnel

In this section

🍪 We use cookies to personalize and enhance your experience on our site. Visit our Privacy Policy to learn more.

BY USING OUR SITE, YOU AGREE TO OUR USE OF COOKIES.