Docs

Docs

menu-to-close

JoinChat visibility by country

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.

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' );
Code language: PHP (php)

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.

🌟 To seamlessly integrate country visibility into Joinchat with page cache support, you can purchase Joinchat Freelance or Agency and request the Joinchat Country Visibility add-on.

country visibility
JoinChat visibility by country