Banner
WordPress Tutorials

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.

How to Display a 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
$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 in your post, page, or in a sidebar widget.

[show_ip]

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.

 

Article written by dudecmsadmin

dudecmsadmin

Hi there! I`m admin of ThemeGrizzly.com community where I share everything from web. I'm interested more in web design.

Leave a Reply

Your email address will not be published. Required fields are marked *

Banner