2

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.

j8d
  • 446
  • 7
  • 23
  • 2
    "the code is just causing most of the content on a page to disappear" > Check you error logs, activate your error reporting, set your error reporting level. After you find out what's the error thrown there, either you figure it out yourself, or you can add the error to the question. – Alin Purcaru Feb 11 '16 at 13:49
  • Ok, will look up how to check the error logs. Also, if I put the code in the file outside of a function I get a server error. – j8d Feb 11 '16 at 14:35

1 Answers1

0

Got it. I had to create a namespace for the .dat file.

    $gi = \LSMIGeoIP\geoip_open(WP_PLUGIN_DIR . "/location-specific-menu-items/GeoIP.dat", GEOIP_STANDARD);
    $user_country = \LSMIGeoIP\geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    \LSMIGeoIP\geoip_close($gi);
j8d
  • 446
  • 7
  • 23