How To Get Users IP Addresses On WordPress And Display it Using Shortcodes

We have been working with some of our users to get them set up with Geolocation in various ways, we have more to come on this in later posts, however in the meantime, one of the key components you need to geolocate a user to get the IP address.

In this post, we will show you how to display a user’s IP address in WordPress. There are numerous possibilities that come from the use of this technology including legal notices, customization, currency, weather, security and much more.

To give a little background, an IP address which is actually short for Internet Protocol address is a unique identifying number for a piece of network hardware. This hardware can be a computer, a router, a cell phone, a webcam among others.

Having an IP address allows a device to communicate with other devices over an IP-based network like the internet. It’s essentially the postal addresses of computers.

moving on to how to get the IP address in WordPress, from a very high level, all you have to do is paste the code snippet in your theme’s functions.php file or in a site-specific plugin and add a shortcode filter like in the code posted below.

function get_the_user_ip()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        //check ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        //to check ip is pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return apply_filters('wpb_get_ip', $ip);
}

//this creates the shortcode you can use in posts, pages and widgets
add_shortcode('show_user_ip', 'get_the_user_ip');

To explain the code above, we are basically, reading all the server information that comes with a request comes from the user (this happens when someone visits your website) and looking at 3 key main values from the $_SERVER global object which are likely to contain the IP address.

Related:  How to Speed Up Your WordPress Site By Disabling Smileys, Emojis and Emoticons

We first check the HTTP_CLIENT_IP value, if it doesn’t contain any values we move on to the HTTP_X_FORWARDED_FOR, if that also fails we then resort to the REMOTE_ADDR. There are subtle differences between these fields which we won’t cover in this post.

After you’ve completed this task, you just want to add the right shortcode within a page, post, sidebar widget or anywhere else on your site. The shortcode is:

[show_user_ip]

Adding this to a sidebar widget is done with the text widget. Go to Appearance >> Widgets and add the text widget to your sidebar. Then, add the shortcode to the text widget.

Depending on your setup or theme you may have to enable shortcodes for your widgets, as well. You will need to go back to your functions.php file to do this. Simply access this file again and add the following code and save the file.

add_filter('widget_text', 'do_shortcode');

Now you will be able to use any shortcode within the text widget in your sidebar. This will make it possible to display the IP address of the visitor coming to your website.

In this post, we covered the basics of what an IP address is and provided the code snippet you need to display the IP address of a visitor to your website. If you enjoyed reading or you find it useful feel free to leave a comment or share the post with you friends.