PHP Integration

If you want to use Geo Controller data inside your PHP files or custom templates, there are several flexible integration methods available.

1. Using do_shortcode()

You can render any Geo Controller shortcode using the native WordPress do_shortcode() function:

echo do_shortcode('[cfgeo return="city"]');

2. Using PHP Classes and Helpers

The legacy CF_Geoplugin class is deprecated. Please use the new CFGP_U and CFGP_API classes instead.

// Preferred usage
$data = CFGP_U::api();              // Full structured geodata array
$country = $data['country'] ?? '';  // Safe access to fields

// Access a single field from the API via helper
$country = CFGP_U::api('country');      // e.g. "Germany"
$countryCode = CFGP_U::api('country_code');

// Explicit lookup via the API class (advanced)
$response = CFGP_API::lookup('8.8.8.8', ['dns' => true]);
// Always validate keys defensively
if (is_array($response)) {
    $city = $response['city'] ?? '';
}

For more information about deprecated code and migration details, please read:
Deprecated Code Notice

3. Using Global Variables

You can also access location data using WordPress global variables provided by the plugin:

Array style:

global $CFGEO;
echo $CFGEO['city'];

These global variables are automatically populated when the plugin initializes and are available throughout WordPress templates and theme files.

Note: Always ensure the plugin is active and loaded before calling its classes or globals, especially in custom environments or headless implementations.

Was this page helpful?