I wrote a function inside a class that gets a user's country from the IP address...

function getcountry() {
    if (!class_exists('GeoIP')) {

    if (empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
        $ip_address = $_SERVER["REMOTE_ADDR"];
    } else {
        $ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];

    $GeoIPv4_file = plugin_dir_path( __FILE__ ) . 'data/GeoIPv4.dat';
    $GeoIPv6_file = plugin_dir_path( __FILE__ ) . 'data/GeoIPv6.dat';

    if (!filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE) {     
        if ( is_readable ( $GeoIPv4_file ) ) 
        $gi = \LSMIGeoIP\geoip_open( $GeoIPv4_file, GEOIP_STANDARD );
        $user_country = \LSMIGeoIP\geoip_country_code_by_addr($gi, $ip_address);
    } elseif (!filter_var($ip_address, FILTER_VALIDATE_IP,FILTER_FLAG_IPV6) === FALSE) {
        if ( is_readable ( $GeoIPv6_file ) )
        $gi = \LSMIGeoIP\geoip_open( $GeoIPv6_file, GEOIP_STANDARD );
        $user_country = \LSMIGeoIP\geoip_country_code_by_addr($gi, $ip_address);
    } else {
        $user_country = "Can't locate IP: " . $ip_address;              
    return $user_country;

but calling the function within another function...

function set_visibility( $items, $menu, $args ) {
    $user_country = getcountry();

returns an error: Call to undefined function getcountry()

If I take the code inside the function and place it inside the other function, it successfully retrieves the user's country.

What's my noob mistake?

  • 1
    Try this: $user_country = $this->getcountry(); (If the function set_visibility is within the same class).
    – Domain
    Feb 22, 2016 at 5:43

1 Answer 1


You can try following while you get such error:

  • Searched for the php file with the function definition in it. Verified that the file exists.

  • Verified that the require (or include) statement for the above file exists in the page. Also, verified the absolute path in the
    require/include is correct.

  • Verified that the filename is spelled correctly in the require statement.

  • Echoed a word in the included file, to see if it has been properly included.

  • Defined a separate function at the end of file, and called it. It worked too.

Problem :

  • Your function is probably in a different namespace than the one you're calling it from.

Solution :

$this-> refers to a function that belongs to the class and is declared within the class, same thing applies for variables declared inside the class. Inside the class it's called $this->function(), If public outside the class it's called $theclass->function()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.