REST API Endpoint

AffLoc registers a custom WordPress REST API endpoint for geolocation detection.

Endpoint

GET /wp-json/aff-loc/v1/country

Response

{
  "country": "GB"
}

How It Works

  1. The frontend JavaScript calls this endpoint when the visitor’s country is not cached in localStorage.
  2. The endpoint reads the visitor’s IP from the request headers.
  3. IP detection priority:
    • HTTP_CF_CONNECTING_IP (Cloudflare)
    • HTTP_X_FORWARDED_FOR (Proxies/Load balancers)
    • REMOTE_ADDR (Direct connection)
  4. The IP is sent to https://ipapi.co/{ip}/json/ for country lookup.
  5. The two-letter country code is returned.

Error Handling

  • If the IP is invalid or localhost (127.0.0.1, ::1), returns US as default.
  • If the geolocation API call fails, returns default.

Authentication

This endpoint is publicly accessible (permission_callback: __return_true) because it needs to work for anonymous front-end visitors. It only returns a country code — no sensitive data.