Colorful pins on a map of Italy

How to Get and Display a User’s IP Address in WordPress

Do your users need to know their IP addresses in order to configure one of your products? Or is your site plagued by spam comments, and nothing seems to stop them? Both of these very different scenarios call for the same solution: you’ll need to find and display IP addresses.

IPs are the street addresses of the internet. When computers need to share ‘packages’ of data with each other, they need to know where to send the information, and that’s just what IP addresses are for. Thanks to its huge array of powerful tools, WordPress makes it easy to find these addresses, no matter why you need them.

In this article, we’ll cover how to enable users to see their own IP address, and how to track your users’ IPs as an administrator. Let’s get to work!

Why Display a User’s IP Address?

Generally, users only need to see their own IP addresses if they need them for a specific purpose. Some examples might include:

  • Configuring a new network
  • Setting up new networking equipment
  • Whitelisting themselves on a website or network
  • Providing personal information to an application

Websites that display the user’s IP are very useful in these situations, so it could be a valuable service to provide for your visitors. There are even sites dedicated entirely to this purpose. If your site hosts software that requires users to enter their IPs, displaying that information is a smart way to provide extra value.

Most of the reasons an administrator would need a user’s IP address revolve around website security. Blacklisting the IP addresses of troublemakers on forums and preventing spam comments are common scenarios where you’d want to know a user’s IP. In these cases, blocking the IP prevents the user from simply making a new account to bypass a ban.

How to Find a User’s IP Address in WordPress

Finding the IP of a user who made a comment on your WordPress site is relatively simple. First, head to your WordPress dashboard and navigate to the Comments section in the sidebar. This will display all of the comments on your site:

Screenshot showing how to find a user's

The IP address of each commenter can be found in the Author column, under their username and email address. You can filter the display to only show certain types of comments if you have a lot to sort through. There’s also a handy search bar if you know the username you’re looking for, or some of the relevant comment’s text.

Popular forum plugins, such as BuddyPress and bbPress, may also enable you to view the IP addresses of posters. You’ll want to consult the documentation for your specific plugin for more details.

How to Display a User’s IP Address (To Them)

Displaying a WordPress user’s IP address so that they can see it is fairly simple. You’ll just need to add a few lines of code to your theme’s functions.php file. Then you’ll use a shortcode to display the IP address on a page or post.

Here’s the code you’ll need:

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('display_ip', 'get_the_user_ip');

This code creates the display_ip shortcode, and tells it to retrieve the user’s IP address when the code is run.

Step 1: Add the Code to functions.php

From your WordPress dashboard, navigate to Appearance > Theme Editor and select Theme Functions from the list on the right. This is the functions.php file for your theme:

Screenshot showing how to add code to functions.php in WordPress theme editor

Copy the code snippet above, and paste it at the bottom of this file:

Screenshot showing where to paste code snippet

Then click on Update File at the bottom of the page. 

Step 2: Add the Shortcode to Your Page or Post

Next, you’ll need to insert the display_ip shortcode onto the page where you want to display the IP address. There are a few options here: you can insert the shortcode directly into a post or page, or place it in a widget so that it shows up in the footer or sidebar on all pages.

For our example, we’ll place the shortcode on a page using a widget:

Screenshot showing how to place shortcode on a page using a widget in WordPress

To do this, open up the page in the Block Editor, add a block using the ‘plus’ icon, and search for “shortcode”. Then just type the shortcode you created into the field. Save your changes and you’re done!

How to Track a User’s IP Address

If you need to track a user’s IP address for security or administrative purposes, a WordPress plugin is your best bet. There are several options available, but for this example we’ll use TraceMyIP Visitor IP Tracker:

Screenshot showing TraceMyIP Visitor IP Tracker logo

TraceMyIP is a free plugin and service, which enables site administrators to monitor and track the IP addresses that visit their pages in detail. This information can then be used to block IPs that are problematic for the site, such as spammers, forum trolls, and malicious login attempts. 

Step 1: Install the Plugin

To get started, go to your WordPress dashboard, navigate to Plugins > Add New, and search for “TraceMyIP”. Install and activate the plugin.

You’ll now have an entry in your dashboard called TraceMyIP. Clicking on this brings you to the plugin’s dashboard, where you can sign up for an account. There is a free option that includes most of the features you’re likely to need.

Step 2: Add the Tracking Code

Sign up for an account, and follow the instructions you receive next. Eventually you’ll get a code that you need to paste into the header section of your theme.

To do this, go to your dashboard and navigate to Appearance > Theme Editor. Select the Theme Header (header.php) file from the list on the right, and paste the code at the bottom of the file:

Screenshot showing how to add tracking code to your Theme Header in WordPress

Once that’s done, the plugin will start tracking visitors. To view this activity, head to the plugin’s dashboard and select My Projects. You’ll see an overview of all the information that’s been collected, which you can sort through to get more details:

Screenshot showing how to track visitor activity using the TraceMyIP plugin for WordPress

Now you’re all set to track the IP addresses that visit your site.

Do More with WP Engine

The ability to pull IP addresses from your site’s visitors is useful in many different scenarios, from helping users to blacklisting attackers. WordPress’ flexibility makes accomplishing either of these tasks easy.

If you need your site to do more, WP Engine can help. We offer the best resources on the web for WordPress developers, along with flexible hosting plans to help you deliver incredible digital experiences to your users!

Get started.

Build faster, protect your brand, and grow your business with a WordPress platform built to power remarkable online experiences.

Chat with us!