How to Display a User’s IP Address in WordPress

How to Display a User's IP Address in WordPress

In this article, I will show you how to display visitors’ IP address in WordPress. You can also use this php code 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 php code in your theme’s functions.php

  1. Log into your WordPress Admin interface.
  2. Go to Appearance.
  3. Go to Theme Editor.
  4. In the right menu find functions.php
  5. Copy and paste the code below at the end of functions.php
  6. Publish.
/** * Dispaly Users Ip*/
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');

Next thing you need to do is add the following shortcode [show_ip] in your post, page, or in a sidebar widget. Unfortunately, there is still a weakness of the function, that is the problem of  “cache”. The visitor will see the same IP address (even if the router is restarted) when they are not clearing “cache” data in the browser.

Will the IP be saved or stored in WordPress? Absolutely not, the IP will only be visible to the visitor.

This post was updated on Friday / August 2nd, 2019 at 3:45 PM