Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Display a User’s IP Address in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Display a User’s IP Address in WordPress

Recently one of our users asked us if it was possible to detect and display individual visitors’ IP address in WordPress. Our answer was of course it is. In this article, we will show you how to display user’s IP address in WordPress.

You can use this to create your own IP detection site. This way when a website visitor views your site, they can see their own IP address.

All you have to do is paste the following snippet in your theme’s functions.php file or in a site-specific plugin.

// Display User IP in WordPress

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

add_shortcode('show_ip', 'get_the_user_ip');

Next thing you need to do is add the following shortcode in your post, page, or in a sidebar widget.


If you’re using the shortcode in your sidebar text widget, and it is not working, then you need to make sure that you enable shortcode for sidebar widgets.

We hope this article helped you display user’s IP address in your WordPress site. If you have any questions or feedback, then please leave us a comment below.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. Brigitte Scherieble says:

    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?

  2. Evan says:

    This works great, thank you! Is there any way to have it also display the hostname, perhaps with a separate shortcode?

  3. Reuben Tinto says:


    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! :)


  4. Mike says:

    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 says:

      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.

    • Mohammad Mursaleen says:

      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;

  5. Vivek Digarse says:

    Thanks !

  6. John says:

    Thanks for this, works fine.
    But is it possible to use this to monitor new user subscriptions?

  7. alexander kochubey says:

    Thanks for reply, I’ll modify some plugin for some system, I’ll describe it later

  8. meera says:

    Thank you so much…. it works fine……..

  9. Abhisek Padhi says:

    @pippin you are correct. Your code worked fine, but wpbeginner’s code was also working fine.

  10. Doug Hall says:

    Display it where? I don’t understand the post at all.

  11. Pippin Williamson says:

    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
    } else {
    $ip = $_SERVER[‘REMOTE_ADDR’];
    return apply_filters( ‘edd_get_ip’, $ip );

  12. Chris Black says:

    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.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.