User IP and Location Shortcode

Below, you’ll find a detailed guide on how to add the User IP and Location Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the User IP and Location Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the User IP and Location Plugin and the shortcodes it provides:

✩✩✩✩✩ () Active Installs: + Tested with: PHP Version:
Included Shortcodes:

User IP and Location [userip_location] Shortcode

The User IP and Location shortcode allows you to display various user data on your WordPress site. It’s customizable, enabling you to show information like IP, location, browser, and more. By using this shortcode, you can access user data such as the continent, country, region, city, latitude, longitude, timezone, currency, ISP, mobile, proxy, hosting, browser, operating system, and even the user’s country flag. It’s a versatile tool for personalizing and optimizing your website content.

Shortcode: [userip_location]

Parameters

Here is a list of all possible userip_location shortcode parameters and attributes:

  • type – determines what information to display, for instance, IP, country, or browser.
  • height – sets the height of the country flag image displayed.
  • width – sets the width of the country flag image displayed.

Examples and Usage

Basic example – Displaying the user’s IP address using the shortcode

[userip_location type="ip"]

Advanced examples

Displaying the user’s country and customizing the height and width of the output

[userip_location type="country" height="100px" width="200px"]

Showing the user’s browser information

[userip_location type="browser"]

Displaying the user’s country flag with a custom height and width

[userip_location type="flag" height="50px" width="50px"]

Getting the user’s timezone

[userip_location type="timezone"]

Showing whether the user is using a mobile device or not

[userip_location type="mobile"]

Displaying the user’s Internet Service Provider (ISP)

[userip_location type="isp"]

Note: The shortcode will return “Invalid type” if the type attribute is not one of the predefined options.

PHP Function Code

In case you have difficulties debugging what causing issues with [userip_location] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode("userip_location", "user_ip_and_location"); // Add the shortcode.

Shortcode PHP function:

function user_ip_and_location($atts)
{
    // Extract the shortcode attributes.
    extract(
        shortcode_atts(
            [
                "type" => "",
                "height" => "auto",
                "width" => "50px",
            ],
            $atts
        )
    );

    // Convert the shortcode attribute to lowercase.
    $type = strtolower(sanitize_text_field($type));

    // Initialize the IP and Browser objects.
    $ip = new User_IP_and_Location();
    $browser = new User_Browser();

    // Switch statement to determine the type of information to display.
    switch ($type) {
        case "ip":
            return $ip->getIP();
        case "continent":
            return $ip->getContinent();
        case "country":
            return $ip->getCountry();
        case "countrycode":
            return $ip->getCountryCode();
        case "region":
            return $ip->getRegion();
        case "regionname":
            return $ip->getRegionName();
        case "city":
            return $ip->getCity();
        case "lat":
            return $ip->getLat();
        case "lon":
            return $ip->getLon();
        case "timezone":
            return $ip->getTimezone();
        case "currency":
            return $ip->getCurrency();
        case "isp":
            return $ip->getISP();
        case "mobile":
            return $ip->getMobile();
        case "proxy":
            return $ip->getProxy();
        case "hosting":
            return $ip->getHosting();
        case "browser":
            return $browser->get_browser_name();
        case "os":
            return $browser->get_operating_system();
        case "flag":
            // Get the user's country flag.
            $flag_country = $ip->getflag() ?: "us";
            $flag_url = sprintf(
                "%s/%s/flags/%s.png",
                plugins_url(),
                explode('/', plugin_basename(__FILE__))[0],
                strtolower($flag_country)
            );
            $flag_html = sprintf(
                '<img src="%s" style="height:%s!important;width:%s!important;" onmouseover="%s">',
                esc_url($flag_url),
                esc_attr($height),
                esc_attr($width),
                esc_attr('') // Sanitized onmouseover attribute
            );
            return $flag_html;
        default:
            return "<p>Invalid type</p>";
    }
}

Code file location:

user-ip-and-location/user-ip-and-location/inc/user-ip-functions.php

Conclusion

Now that you’ve learned how to embed the User IP and Location Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *