/*You can add more countries using array('US', 'CA'); */ function block_visitors() { $blocked_countries = array('US'); $ip = $_SERVER['REMOTE_ADDR']; $api_url = "http://www.geoplugin.net/json.gp?ip=".$ip; $visitor_data = @file_get_contents($api_url); if ($visitor_data === FALSE) { error_log('Failed to retrieve geolocation data for IP: ' . $ip); return; } $visitor_data = json_decode($visitor_data, true); if (json_last_error() !== JSON_ERROR_NONE) { error_log('Failed to decode geolocation data for IP: ' . $ip); return; } if (!isset($visitor_data['geoplugin_countryCode'])) { error_log('Country code not found in API response for IP: ' . $ip); return; } $visitor_country = $visitor_data['geoplugin_countryCode']; if (in_array($visitor_country, $blocked_countries)) { wp_die('Sorry, access to this website is blocked from your country.'); } } add_action('init', 'block_visitors');