Many of our readers ask us how to track user activity on their WordPress websites. But here’s another scenario: what if the user needs their own IP address?
Maybe you sell a product that requires the customer to enter their IP address, or you write tutorials that deal with IP addresses and similar security topics. In that case, displaying the user’s unique IP address on your site can be a big time-saver and improve the user experience.
In this article, we will show you how to display a user’s IP address on your WordPress website.
Why Display a User’s IP Address in WordPress?
An IP (Internet Protocol) address is a string of numbers separated by periods that is used to identify hardware devices on a network. This means that the visitor’s IP address simply identifies the device that they’re using to connect to the internet, such as their smartphone, computer, or tablet.
There are many cases where visitors may need to know their IP address. For example, sometimes users will need to type in their unique IP address when setting up a piece of software, particularly WordPress security plugins such as firewalls.
If you sell this kind of software, then showing the user’s unique IP address in your online documentation can help customers set up their new software.
Even if you don’t sell the software yourself, many WordPress bloggers publish helpful guides on how to use third-party software. If you’re writing a tutorial where the reader needs to type in their IP address, then showing the exact IP address they need to use can help you create a much better tutorial.
In this post we have several ways to show the user’s IP address on your WordPress website. If you prefer to jump straight to a particular method, then you can use the links below.
- How to display a visitor’s IP address in WordPress using a plugin
- How to display a visitor’s IP address in WordPress using code
1. How to Display a Visitor’s IP Address in WordPress Using a Plugin
The easiest way to display a user’s IP address in WordPress is by using the User IP and Location plugin. This is a simple plugin that lets you show the visitor’s IP address on any page, post, or widget-ready area using a shortcode.
First, you’ll need to install and activate the plugin. If you need help, then please see our guide on how to install a WordPress plugin.
Upon activation, simply open the page, post, or widget-ready area where you want to show the visitor’s IP address. Then, click the ‘Plus’ add block icon and search for ‘Shortcode.’
When the right block appears, click to add it to your WordPress website.
Now, simply type in the [userip_location type=ip] shortcode. For a more detailed look at using shortcodes, see our beginner’s guide on how to add a shortcode in WordPress.
Once you’ve done that, click on the ‘Update’ or ‘Publish’ button to save your changes.
Now if you visit this page you’ll see your unique IP address.
2. How to Display a User’s IP Address in WordPress Using Code
You can also display a visitor’s IP address using code. However, we don’t recommend adding this code directly to your site, as even a small mistake can cause common WordPress errors. Also, you’ll lose this custom code the next time you update your WordPress theme.
Instead, we will be using WPCode, the easiest and safest way for anyone to add custom code to a WordPress website.
To start, you’ll need to install and activate WPCode. For more information, see our step-by-step guide on how to install a WordPress plugin.
After that, go to Code Snippets » Add Snippet in the WordPress dashboard.
Here, you’ll see all the pre-made snippets you can add to your site. This includes a snippet that allows you to completely disable comments, upload file types that WordPress doesn’t usually support, disable attachment pages, and much more.
To display the user’s IP address, hover over ‘Add Your Custom Code’ and then select ‘Use snippet.’
To start, type in a title for the custom snippet. This can be anything that helps you identify the code in your WordPress dashboard.
With that done, open the ‘Code Type’ dropdown menu and choose ‘PHP Snippet.’
After that, simply paste the following snippet into the code editor:
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 );
}
add_shortcode('show_ip', 'get_the_user_ip');
To make the snippet live, scroll to the top of the screen and click on the ‘Inactive’ toggle so it changes to ‘Active.’
Finally, click on ‘Save Snippet’ to make the snippet live.
You’ve now created a [show_ip] shortcode that you can use to show the visitor’s IP address on any page, post, or widget-ready area.
For a step by step guide on how to place the shortcode, see our beginner’s guide on how to add a shortcode in WordPress.
We hope this article helped you learn how to display a user’s IP address in your WordPress site. You may also want to check out our guide on how to allow user registration on your WordPress site and the best GDPR plugins to improve compliance.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Syed Balkhi
Hey WPBeginner readers,
Did you know you can win exciting prizes by commenting on WPBeginner?
Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
You can get more details about the contest from here.
Start sharing your thoughts below to stand a chance to win!
Christopher
Great article! Is it also possible to show visitors country?
WPBeginner Support
That would require some advanced php or a plugin, we don’t have a recommended method at the moment.
Admin
Lathif
unfortunately there is still a weakness of the function, that is the problem of “cache”.
visitors will see the same ip address (even if the router is restarted) when they are not clearing “cache” in the browser.
Pamela
Is there a way to display the user’s country flag with this code?
Thanks.
Lloyd
How about a city or state instead of IP?
Beatriz
Where can I see the IP’s collected? and should I paste the snippet of the code on the top of my function.php code?
WPBeginner Support
Paste the code at the bottom of your functions.php file. You will have to use shortcode
[show_ip]
inside a text widget. See our guide on how to add and use widgets in WordPress.Admin
Brigitte Scherieble
hey,
I am from Germany and I am not a computer freak. But I have the job to find out of the client IP (out of Google Analytics) the Company of the visiter. Can you help me?
Evan
This works great, thank you! Is there any way to have it also display the hostname, perhaps with a separate shortcode?
Reuben Tinto
Hi,
Is it possible to obtain the IP of a previous blog visitor trough this method? If not are you aware of any other courses of action I can take?
Also will I be able to access code if I do not have a premium wordpress account?
I have been trying to locate the IP of an anonymous twitter troll and would really appreciate some help!
Reuben
Mike
Thanks for the tutorial. Please excuse if this is a dumb question but I’m new to working with WordPress.
If I want to store the visitors IP address along with the date and time they visited a post, should I create a separate mysql database table or is there an existing wp table that collects this information that I can retrieve?
WPBeginner Support
WordPress only stores vistor’s information in the database if they decide to leave a comment. You can create a separate database to collect this information if you want.
Admin
Mohammad Mursaleen
Creating a table for such functionality in WordPress is not an efficient move. I would prefer to do this using custom post type.
I have answered a similar question over here;
Vivek Digarse
Thanks !
John
Thanks for this, works fine.
But is it possible to use this to monitor new user subscriptions?
alexander kochubey
Thanks for reply, I’ll modify some plugin for some system, I’ll describe it later
meera
HI
Thank you so much…. it works fine……..
Abhisek Padhi
@pippin you are correct. Your code worked fine, but wpbeginner’s code was also working fine.
Doug Hall
Display it where? I don’t understand the post at all.
as
Everywhere use shortcode [show_ip]
Pippin Williamson
It should probably be noted that $_SERVER[‘REMOTE_ADDR’] will not always retrieve the IP properly.
If the user is on a shared connection, you will need to use $_SERVER[‘HTTP_X_FORWARDED_FOR’].
If the user is behind a proxy, you will need to use $_SERVER[‘HTTP_X_FORWARDED_FOR’].
The IP can also be present in $_SERVER[‘HTTP_CLIENT_IP’].
To properly get the IP reliably, something like this should be used:
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( ‘edd_get_ip’, $ip );
}
Pippin Williamson
Formatted version: https://gist.github.com/pippinsplugins/9641841
WPBeginner Support
Thanks Pippin, we have updated the article.
Admin
Chris Black
Always worth noting that although you can read the IP address with PHP that it may not be useful because people have shared IP addresses, dynamic IP, access websites from different networks/locations and because it is possible to fake the IP address that PHP sees. So – only use an IP address with caution.