Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressでユーザーのIPアドレスを表示する方法

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

WordPressでユーザーのIPアドレスを表示したいですか?

ユーザーが製品の設定やチュートリアルを完了するためにIPアドレスを知る必要がある場合、使用する必要がある正確なIPアドレスを表示するのに役立ちます。

この投稿では、WordPressサイトにユーザーのIPアドレスを表示する方法を紹介します。

How to display a user's IP address in WordPress

なぜ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サイトに追加します。

Showing the visitor's IP address on your WordPress website using a shortcode

あとは、[userip_location type=ip]ショートコードを入力するだけです。ショートコードの使い方については、WordPressでショートコードを追加する方法の初心者向けガイドをご覧ください。

更新」または「公開」ボタンをクリックして変更を保存します。

Showing a visitor's IP address on your WordPress website

このページにアクセスすると、あなた固有の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.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

25件のコメント返信を残す

  1. 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!

    • WPBeginner Support says

      That would require some advanced php or a plugin, we don’t have a recommended method at the moment.

      管理者

  2. 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.

  3. 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?

  4. 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?

  5. Evan says

    This works great, thank you! Is there any way to have it also display the hostname, perhaps with a separate shortcode?

  6. 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

  7. 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;

  8. 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 );
    }

  9. 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.

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。