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でログイン中のユーザー情報を取得してパーソナライズされた結果を得る方法

先日、お気に入りの投稿をパーソナライズされたライブラリに保存することで、ユーザーにパーソナライズされた体験を提供する方法を紹介しました。パーソナライズされた結果をさらにレベルアップするには、ユーザーのファーストネームを使用する必要があります(例:ウェルカム画面)。幸運なことに、WordPressはログイン中のユーザーの情報をとても簡単に取得することができます。この投稿では、現在ログイン中のユーザーに関する情報を取得する方法を紹介します。

get_currentuserinfo();関数を利用します。この関数は、テーマの任意の場所(ヘッダー、フッター、サイドバー、ページテンプレートなど)で使用できます。これが機能するためには、ユーザーがログイン中でなければなりません。そのため、is_user_logged_in()という条件文を使用する必要があります。コード例

[cbk1]

ログインしているユーザーに対して、例えば「Hey Syed, Everything is here, right where you hoped it would be」のようなカスタマイザーメッセージを表示することができます。上記のコードは次のようになります:

[cbk2]

上記で追加したマジックコードは$current_user->user_firstname;です。これは、get_currentuserinfo()の呼び出しが現在のユーザーの情報を$current_userに入れるために動作しています。同様のメソッドを使用して、ログイン名やユーザ ID、メール、サイトなどのユーザ情報を取得することができます。

すべての情報の使用例を以下に示します:

[cbk3]

参考になれば幸いです。お気に入りの投稿を追加する機能と組み合わせることで、パーソナライズされた体験を簡単に作り出すことができます。

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

アバター

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

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

  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!

  2. S Meredith says

    Hi Syed,
    Fantastic site.
    It’s helped me a lot with understanding WP and editing my own site.
    My question is, once I’ve inserted the above function into my child themes functions.php, how would i create a shortcode so that I can then display this user information on a generic page?

    Are shortcodes the best way to allow this to be displayed on any page i’d like.

    For example, I have a static page that I would like to display ‘Welcome {user_name}’ and their profile picture.

  3. Pavan says

    I know nothing about php. I just want to show the username of my customer on TOP bar navgation, and from there he can access his account. How do I do that.

  4. Faruk says

    What if the user not logged in. Can the wordpress still send personalised email with his name? and if sa what is the variable and how?

返信を残す

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