¿Desea mostrar la dirección IP de un usuario en WordPress?
Si sus usuarios necesitan conocer su dirección IP para configurar uno de sus productos o completar su tutorial, entonces es útil mostrarles la dirección IP exacta que necesitan utilizar.
En este artículo, le mostraremos cómo mostrar la dirección IP de un usuario en su sitio web WordPress.
¿Por qué mostrar la dirección IP de un usuario en WordPress?
Una dirección IP (Protocolo de Internet ) es una cadena de números separados por puntos que se utiliza para identificar dispositivos de hardware en una red. Esto significa que la dirección IP del visitante simplemente identifica el dispositivo que está utilizando para conectarse a Internet, como su smartphone, ordenador o tableta.
Hay muchos casos en los que los visitantes pueden necesitar conocer su dirección IP. Por ejemplo, a veces los usuarios tendrán que escribir su dirección IP única al establecer una pieza de software, en particular los plugins de seguridad de WordPress, tales como cortafuegos.
Si vende este tipo de software, mostrar la dirección IP exclusiva del usuario en su documentación en línea ayudará a sus clientes a establecer su nuevo software.
Aunque no vendas el software tú mismo, muchos blogueros de WordPress publican guías útiles sobre cómo utilizar software de terceros. Si estás escribiendo un tutorial en el que el lector tiene que escribir su dirección IP, mostrar la dirección IP exacta que tiene que utilizar puede ayudarte a crear un tutorial mucho mejor.
En esta entrada tenemos varias formas de mostrar la dirección IP del usuario en su sitio web WordPress. Si prefiere saltar directamente a un método en particular, entonces usted puede utilizar los siguientes enlaces.
- Cómo mostrar la dirección IP de un visitante en WordPress utilizando un plugin
- Cómo mostrar la dirección IP de un visitante en WordPress utilizando un código
1. Cómo mostrar la dirección IP de un visitante en WordPress utilizando un plugin
La forma más sencilla de mostrar la dirección IP de un usuario en WordPress es usando el plugin User IP and Location. Se trata de un simple plugin que te permite mostrar la dirección IP del visitante en cualquier página, entrada o área preparada para widgets mediante un shortcode.
En primer lugar, tendrás que instalar y activar el plugin. Si necesitas ayuda, consulta nuestra guía sobre cómo instalar un plugin de WordPress.
Una vez activado, sólo tienes que abrir la página, entrada o zona preparada para widgets en la que quieras mostrar la dirección IP del visitante. A continuación, haz clic en el icono “Más” para añadir un bloque y busca “Shortcode”.
Cuando aparezca el bloque adecuado, haga clic para añadirlo a su sitio web de WordPress.
Ahora, simplemente escriba el shortcode [userip_location type=ip]. Para obtener más información sobre el uso de shortcodes, consulte nuestra guía para principiantes sobre cómo añadir un shortcode en WordPress.
Una vez hecho esto, haz clic en el botón “Actualizar” o “Publicar” para guardar los cambios.
Ahora, si visitas esta página, verás tu dirección IP única.
2. Cómo mostrar la dirección IP de un usuario en WordPress usando código
También puede mostrar la dirección IP de un usuario añadiendo un código a su blog o sitio web de WordPress. Este código creará un shortcode único que puedes colocar en cualquier lugar de tu sitio.
Es tan sencillo como copiar y pegar un código en su sitio. Sin embargo, siempre es una buena idea crear una copia de seguridad antes de hacer cambios en el código de su sitio web.
Si aún no ha realizado una copia de seguridad de su sitio web en WordPress, puede consultar nuestra selección de expertos de los mejores plugins de copia de seguridad para WordPress.
Para mostrar la dirección IP del usuario, abra el archivo functions.php de su tema, cree un plugin específico para el sitio o utilice un plugin de fragmentos de código.
Independientemente de la opción que elija, sólo tiene que pegar el siguiente código:
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');
Una vez hecho esto, active el plugin específico de su sitio, guarde el fragmento de código o guarde los cambios realizados en el archivo functions.php de su sitio.
Ahora ha creado un shortcode [show_ip] que puede utilizar para mostrar la dirección IP del visitante en cualquier página, entrada o área preparada para widgets.
Para obtener una guía paso a paso sobre cómo colocar el shortcode, consulte nuestra guía para principiantes sobre cómo añadir un shortcode en WordPress.
Esperamos que este artículo te haya ayudado a aprender cómo mostrar la dirección IP de un usuario en tu sitio WordPress. También puede marcar / comprobar nuestra guía sobre cómo permitir el registro de usuarios en su sitio de WordPress y el mejor software de chat en vivo para las pequeñas empresas.
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 says
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 says
Great article! Is it also possible to show visitors country?
WPBeginner Support says
That would require some advanced php or a plugin, we don’t have a recommended method at the moment.
Administrador
Lathif says
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 says
Is there a way to display the user’s country flag with this code?
Thanks.
Lloyd says
How about a city or state instead of IP?
Beatriz says
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 says
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.Administrador
Brigitte Scherieble says
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 says
This works great, thank you! Is there any way to have it also display the hostname, perhaps with a separate shortcode?
Reuben Tinto says
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 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.
Administrador
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;
Vivek Digarse says
Thanks !
John says
Thanks for this, works fine.
But is it possible to use this to monitor new user subscriptions?
alexander kochubey says
Thanks for reply, I’ll modify some plugin for some system, I’ll describe it later
meera says
HI
Thank you so much…. it works fine……..
Abhisek Padhi says
@pippin you are correct. Your code worked fine, but wpbeginner’s code was also working fine.
Doug Hall says
Display it where? I don’t understand the post at all.
as says
Everywhere use shortcode [show_ip]
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
$ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’];
} else {
$ip = $_SERVER[‘REMOTE_ADDR’];
}
return apply_filters( ‘edd_get_ip’, $ip );
}
Pippin Williamson says
Formatted version: https://gist.github.com/pippinsplugins/9641841
WPBeginner Support says
Thanks Pippin, we have updated the article.
Administrador
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.