slimstat_get_country
Applies to SlimStat 5.5.0 · checked
The slimstat_get_country filter is no longer applied anywhere in SlimStat, so a callback attached to it never runs and cannot change a stored country code. It was dropped during one of the geolocation rewrites and this page is kept rather than deleted, because code written against the old name fails silently rather than erroring.
If you have a slimstat_get_country callback in a theme or plugin today, it is doing nothing. Nothing errors, no notice is logged, and the country column simply carries whatever geolocation resolved. Two hooks replace it, and which one you want depends on whether you are correcting the input or the output.
| What you want to change | Hook to use | Runs |
|---|---|---|
| The IP that geolocation resolves | slimstat_filter_ip_address | Before the lookup |
| The country that was resolved | slimstat_filter_pageview_stat | After the lookup, before the insert |
| Whether the visit is recorded at all | slimstat_filter_pre_tracking | Before anything is collected |
| The geolocation database location | slimstat_maxmind_path | When the database is loaded |
Overriding the country that gets stored
slimstat_filter_pageview_stat receives the complete pageview array immediately before it is written to the database, which makes it the correct place to substitute a country from your own source — a CDN header, an internal lookup service, or a fixed value for a known range.
add_filter( 'slimstat_filter_pageview_stat', function ( $stat ) {
// Two-letter code, lowercase, matching what geolocation would have stored.
if ( ! empty( $_SERVER['HTTP_X_MY_EDGE_COUNTRY'] ) ) {
$stat['country'] = strtolower(
substr( sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_MY_EDGE_COUNTRY'] ) ), 0, 2 )
);
}
return $stat;
} );
Return the array. Returning an empty array from this filter discards the pageview entirely, which is a useful behaviour but not the one you want here.
Correcting the IP instead of the country
Where the country is wrong because the IP is wrong — the usual cause behind a reverse proxy or a load balancer that passes its own address — fix the input rather than the output. slimstat_filter_ip_address runs before geolocation, so a corrected address produces a correct country, a correct city and correct coordinates together.
The filter receives a numerically indexed array, not an associative one: index 0 is the address SlimStat resolved from REMOTE_ADDR, and index 1 is the originating address it found in a forwarding header, or an empty string when there was none.
add_filter( 'slimstat_filter_ip_address', function ( $ip_array ) {
if ( ! empty( $_SERVER['HTTP_TRUE_CLIENT_IP'] ) ) {
$candidate = sanitize_text_field( wp_unslash( $_SERVER['HTTP_TRUE_CLIENT_IP'] ) );
if ( false !== filter_var( $candidate, FILTER_VALIDATE_IP ) ) {
$ip_array[0] = $candidate;
}
}
return $ip_array;
} );
Validate before assigning. The value arrives from a request header, and SlimStat validates the addresses it collects itself — a callback that skips the check hands an unvalidated string straight into the row.
Before writing either callback, check whether a setting already does the job. SlimStat reads Cloudflare’s own location headers natively when the geolocation provider is set to Cloudflare Header, which is covered in using Cloudflare IP geolocation headers, and a blank or wrong country usually has a cause listed in fixing missing or wrong country codes rather than needing any code at all.
For provider choice, precision and the database file itself, see setting up visitor geolocation. The related filter reference is slimstat_filter_pageview_stat.