Zylu WordPress Location API
Zylu WordPress Location API
by Kumaravel Subramani · github.com/zyluco/zylu-wordpress-location-api
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/zyluco/zylu-wordpress-location-api/archive/refs/heads/main.zipA WordPress plugin that detects visitor location and provides REST API endpoints with location information, plus dynamic content toggle functionality based on geographic regions.
Features
- Automatic Location Detection: Detects visitor's location using IP geolocation
- REST API Endpoint: Provides location data via
/wp-json/zylu-location/v1/detect - Dynamic Content Display: Show/hide content based on visitor's region
- Multiple Geolocation Sources: Uses multiple APIs for reliable location detection
- Cookie Caching: Stores location data in cookies to reduce API calls
- Cloudflare Support: Detects Cloudflare IP headers for accurate location
Installation
- Download the plugin files
- Upload the plugin folder to
/wp-content/plugins/ - Activate the plugin through the 'Plugins' menu in WordPress
- The plugin will automatically start detecting visitor locations
File Structure
zylu-location-api/
├── zylu-location-api.php # Main plugin file
└── js/
└── zylu-location.js # Frontend JavaScript
API Endpoint
GET /wp-json/zylu-location/v1/detect
Returns visitor's location information in JSON format.
Response Example:
{
"ip": "203.0.113.0",
"country": "IN",
"region": "Karnataka",
"city": "Bengaluru",
"timezone": "Asia/Kolkata",
"lat": 12.9716,
"lon": 77.5946
}
Dynamic Content Display
The plugin automatically shows/hides content based on visitor's location using CSS classes:
CSS Classes
.india-content- Content shown only to visitors from India.me-content- Content shown only to visitors from Middle East countries.intl-content- Content shown to visitors from all other countries
Middle East Countries Supported
AE, SA, KW, JO, OM, QA, BH, IQ, IR, SY, LB, YE
Usage Example
<div class="india-content">
<h2>Welcome to our India-specific content!</h2>
<p>Special offers for Indian customers...</p>
</div>
<div class="me-content">
<h2>مرحبا بكم في المحتوى الخاص بالشرق الأوسط</h2>
<p>Content tailored for Middle East visitors...</p>
</div>
<div class="intl-content">
<h2>Welcome International Visitors!</h2>
<p>Global content for all other regions...</p>
</div>
Geolocation Sources
The plugin uses multiple geolocation services in order of priority:
- Primary: ipapi.co - High accuracy, HTTPS
- Backup: ip-api.com - Free tier with good coverage
- Fallback: Cloudflare headers (
HTTP_CF_IPCOUNTRY) - When available
JavaScript API
The plugin also exposes location data through JavaScript:
// The location detection runs automatically on page load
// Content is shown/hidden based on visitor's country
// Access the API URL through the localized script
console.log(zyluLocationSettings.apiUrl);
Caching
- Location data is cached in browser cookies as
user_country - Subsequent page loads use cached data to improve performance
- Cookie-based detection reduces API calls and improves page load speed
Security Features
- Sanitizes all input data
- Uses WordPress built-in HTTP API functions
- Handles Cloudflare proxy detection
- Graceful fallback for failed API calls
Requirements
- WordPress 4.7+
- PHP 7.0+
- Active internet connection for geolocation APIs
Configuration
The plugin works out of the box with no configuration needed. Location detection happens automatically when pages load.
Privacy Considerations
- The plugin processes visitor IP addresses for location detection
- IP addresses are not stored permanently
- Consider adding privacy policy disclosure about location detection
- Complies with WordPress coding standards
Development
Hooks and Filters
The plugin is designed to be extensible. You can modify behavior by:
- Hooking into the REST API response
- Modifying the JavaScript behavior
- Adding custom CSS classes for additional regions
Customization Example
// Add custom region detection
add_filter('zylu_location_region_check', function($country) {
// Custom logic for region detection
return $country;
});
Changelog
Version 1.0
- Added multiple geolocation API support
- Improved error handling
- Added cookie caching
- Enhanced Cloudflare support
- Added Middle East region detection
- Improved JavaScript performance
- Better fallback handling
Support
For support, please create an issue in the GitHub repository or contact the author.
Author
Kumar Subramani
License
This plugin is released under the GPL v2 or later license.
This plugin is designed to help WordPress developers create location-aware websites with minimal setup and maximum flexibility.