I want to get a user's country code and compare it with a variable inside a function.
I downloaded the maxMind GEOIP_STANDARD database file 'geoip.dat' and the file 'geoip.inc' and placed them in the root directory.
I then put the following code inside the function.
require_once("geoip.inc");
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
But the code is just causing most of the content on a page to disappear.
Any clues why this might not work? The function works like it's supposed to when I use $user_location = geoip_detect2_get_info_from_current_ip();
and $user_country_code = $user_location->country->isoCode;
instead.
The whole function is below.
function set_visibility( $items, $menu, $args ) {
require_once("geoip.inc");
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
foreach( $items as $key => $item ) {
$hidden_items = array();
$item_parent = get_post_meta( $item->ID, '_menu_item_menu_item_parent', true );
// $user_location = geoip_detect2_get_info_from_current_ip();
// $user_country_code = $user_location->country->isoCode;
$locations_string = get_post_meta( $item->ID, 'locations', true );
$locations_string = strtoupper($locations_string);
$locations_array = explode(',', $locations_string);
$locations_array_trimmed = array_map('trim', $locations_array);
$locations_array_filtered = array_filter($locations_array_trimmed);
$selected_locations = $locations_array_filtered;
$hide_show = get_post_meta( $item->ID, 'hide_show', true );
var_dump($country_code);
if( $hide_show == 'show' && in_array($country_code, $selected_locations ) )
$visible = true;
elseif( $hide_show == 'show' && (!in_array($country_code, $selected_locations ) ) )
$visible = false;
elseif( $hide_show == 'hide' && in_array($country_code, $selected_locations ) )
$visible = false;
else
$visible = true;
if( ! $visible || isset( $hidden_items[$item_parent] ) ) { // also hide the children of hidden items
unset( $items[$key] );
$hidden_items[$item->ID] = '1';
}
}
return $items;
}
Also, it's the same whether I put it before or after the foreach loop. I tried both require and require_once as well.