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
- The frontend JavaScript calls this endpoint when the visitor’s country is not cached in
localStorage. - The endpoint reads the visitor’s IP from the request headers.
- IP detection priority:
HTTP_CF_CONNECTING_IP(Cloudflare)HTTP_X_FORWARDED_FOR(Proxies/Load balancers)REMOTE_ADDR(Direct connection)
- The IP is sent to
https://ipapi.co/{ip}/json/for country lookup. - The two-letter country code is returned.
Error Handling
- If the IP is invalid or localhost (
127.0.0.1,::1), returnsUSas 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.