WordPressでユーザーのIPアドレスを表示したいですか?
ユーザーが製品の設定やチュートリアルを完了するためにIPアドレスを知る必要がある場合、使用する必要がある正確なIPアドレスを表示するのに役立ちます。
この投稿では、WordPressサイトにユーザーのIPアドレスを表示する方法を紹介します。
なぜWordPressでユーザーのIPアドレスを表示するのか?
IP(インターネット・プロトコル)アドレスは、ピリオドで区切られた文字列で、ネットワーク上のハードウェア端末を識別するために使用されます。つまり、訪問者のIPアドレスは、スマートフォン、コンピューター、タブレットなど、インターネットに接続するために使用している端末を識別するだけです。
訪問者が自分のIPアドレスを知る必要がある場合が多々あります。例えば、特にファイアウォールなどのWordPressセキュリティプラグインを設定する際に、ユーザーが固有のIPアドレスを入力する必要がある場合があります。
この種のソフトウェアを販売している場合、オンライン・ドキュメンテーションにユーザー固有のIPアドレスを表示することで、カスタマイザーが新しいソフトウェアをセットアップする際に役立ちます。
自分でソフトウェアを販売していなくても、多くのWordPressブロガーがサードパーティ製ソフトウェアの使い方を親切に公開しています。読者がIPアドレスを入力する必要があるチュートリアルを書いている場合、使用する必要がある正確なIPアドレスを示すことで、より良いチュートリアルを作成することができます。
この投稿では、WordPressサイトでユーザーのIPアドレスを表示する方法をいくつかご紹介します。特定の方法に直接ジャンプしたい場合は、以下のリンクをご利用ください。
1.プラグインを使ってWordPressで訪問者のIPアドレスを表示する方法
WordPressでユーザーのIPアドレスを表示する最も簡単な方法は、User IP and Locationプラグインを使用することです。これは、ショートコードを使用して、任意のページ、投稿日、またはウィジェット対応エリアに訪問者のIPアドレスを表示できるシンプルなプラグインです。
まず、プラグインをインストールして有効化する必要があります。WordPressプラグインのインストール方法については、こちらをご覧ください。
有効化したら、訪問者のIPアドレスを表示したいページ、投稿、またはウィジェット対応エリアを開くだけです。次に、「プラス」追加ブロックアイコンをクリックし、「ショートコード」を検索する。
右のブロックが外観表示されたら、クリックしてWordPressサイトに追加します。
あとは、[userip_location type=ip]ショートコードを入力するだけです。ショートコードの使い方については、WordPressでショートコードを追加する方法の初心者向けガイドをご覧ください。
更新」または「公開」ボタンをクリックして変更を保存します。
このページにアクセスすると、あなた固有のIPアドレスが表示されます。
2.WordPressでコードを使ってユーザーのIPアドレスを表示する方法
WordPressブログやサイトにコードを追加することで、ユーザーのIPアドレスを表示することもできます。このコードはユニークなショートコードを作成し、サイト上の任意の場所に配置することができます。
これは、サイトにコードをコピー&ペーストするだけの簡単な作業です。しかし、サイトのコードを変更する前にバックアップを作成することは常に良いアイデアです。
WordPressサイトをすでにバックアップしていない場合は、WordPressバックアッププラグインのベストセレクションをご覧ください。
ユーザーのIPアドレスを表示するには、テーマのfunctions.phpファイルを開くか、サイト固有のプラグインを作成するか、コード・スニペット・プラグインを使用します。
どのオプションを設定しても、以下のコードを貼り付けるだけでよい:
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');
そうしたら、サイト固有のプラグインを有効化するか、コード・スニペットを保存するか、サイトのfunctions.phpファイルに変更を保存してください。
これで[show_ip]ショートコードが作成され、任意のページ、投稿日、またはウィジェット対応エリアに訪問者のIPアドレスを表示するために使用できるようになりました。
ショートコードの設置方法については、WordPressでショートコードを追加する方法についての初心者向けガイドをご覧ください。
この投稿が、WordPress サイトでユーザーの IP アドレスを表示する方法を学ぶのにお役に立てば幸いです。WordPress サイトでユーザー登録を許可する方法と 中小企業に最適なライブチャットソフトウェアについての ガイドもご覧ください。
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.
管理者
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.管理者
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.
管理者
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.
管理者
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.