WordPress

最終更新日:
公開日:

レシピ

ログインしているユーザー情報を取得する

WordPressでログインしているユーザーの情報や権限を取得する方法について解説します。

この記事のポイント

  • ログイン中のユーザー情報はwp_get_current_user()から取得する
  • ユーザーのIDを指定するときはget_userdata()、またはnew WP_Userを使う
  • ユーザーのメタデータを取得するときはget_user_meta()で取得する

目次

ユーザー情報を取得する

WordPressでは、ログインしているユーザーの情報はWP_Userオブジェクトに格納しています。
こちらのオブジェクトはwp_get_current_user()から取得する、またはログインしているユーザーのIDを指定してget_userdata()か直接new WP_Userを呼び出すと取得することができます。

以下の例では(1)(2)(3)はいずれも変数$userにログイン中のユーザー情報が含まれるWP_Userオブジェクトを取得します。
取得できる情報は同じ内容なので、現在ログインしているユーザーの情報を取得するときは(1)、ログイン中以外のユーザーの情報を取得するときは(2)か(3)を使うようなイメージです。

index.php

<?php

// (1)wp_get_current_user()
$user = wp_get_current_user();

// (2)new WP_User
$user_id = get_current_user_id();
$user = new WP_User($user_id);

// (3)get_userdata()
$user_id = get_current_user_id();
$user = get_userdata($user_id);

$userに含まれるユーザー情報は以下の内容です。

取得できるユーザー情報

object(WP_User)#390 (8) {

	["data"]=> object(stdClass)#409 (10) {
		["ID"]=> string(1) "1" // ユーザーID
		["user_login"]=> string(5) "admin" // ユーザー名
		["user_pass"]=> string(34) "$P$BqM9 ..省略.. kc1zIvW/" // パスワード
		["user_nicename"]=> string(5) "admin" // ニックネーム
		["user_email"]=> string(21) "sample@gray-code.com" // メールアドレス
		["user_url"]=> string(21) "http://localhost" // URL
		["user_registered"]=> string(19) "2022-01-01 10:30:45" // 作成日時
		["user_activation_key"]=> string(0) "" // 有効化キー
		["user_status"]=> string(1) "0" // 状態
		["display_name"]=> string(5) "admin" // ブログ上の表示名
	}
	["ID"]=> int(1) // ユーザーID
	["caps"]=> array(1) { // 権限
		["administrator"]=> bool(true)
	}
	["cap_key"]=> string(15) "wp_capabilities"
	["roles"]=> array(1) { // 権限
		[0]=> string(13) "administrator"
	}
	["allcaps"]=> array(62) { // 許可されている操作
		["switch_themes"]=> bool(true)
		["edit_themes"]=> bool(true)
		["activate_plugins"]=> bool(true)
		["edit_plugins"]=> bool(true)
		["edit_users"]=> bool(true)
		["edit_files"]=> bool(true)
		["manage_options"]=> bool(true)
		["moderate_comments"]=> bool(true)
		["manage_categories"]=> bool(true)
		["manage_links"]=> bool(true)
		["upload_files"]=> bool(true)
		["import"]=> bool(true)
		["unfiltered_html"]=> bool(true)
		["edit_posts"]=> bool(true)
		["edit_others_posts"]=> bool(true)
		["edit_published_posts"]=> bool(true)
		["publish_posts"]=> bool(true)
		["edit_pages"]=> bool(true)
		["read"]=> bool(true)
		["level_10"]=> bool(true)
		["level_9"]=> bool(true)
		["level_8"]=> bool(true)
		["level_7"]=> bool(true)
		["level_6"]=> bool(true)
		["level_5"]=> bool(true)
		["level_4"]=> bool(true)
		["level_3"]=> bool(true)
		["level_2"]=> bool(true)
		["level_1"]=> bool(true)
		["level_0"]=> bool(true)
		["edit_others_pages"]=> bool(true)
		["edit_published_pages"]=> bool(true)
		["publish_pages"]=> bool(true)
		["delete_pages"]=> bool(true)
		["delete_others_pages"]=> bool(true)
		["delete_published_pages"]=> bool(true)
		["delete_posts"]=> bool(true)
		["delete_others_posts"]=> bool(true)
		["delete_published_posts"]=> bool(true)
		["delete_private_posts"]=> bool(true)
		["edit_private_posts"]=> bool(true)
		["read_private_posts"]=> bool(true)
		["delete_private_pages"]=> bool(true)
		["edit_private_pages"]=> bool(true)
		["read_private_pages"]=> bool(true)
		["delete_users"]=> bool(true)
		["create_users"]=> bool(true)
		["unfiltered_upload"]=> bool(true)
		["edit_dashboard"]=> bool(true)
		["update_plugins"]=> bool(true)
		["delete_plugins"]=> bool(true)
		["install_plugins"]=> bool(true)
		["update_themes"]=> bool(true)
		["install_themes"]=> bool(true)
		["update_core"]=> bool(true)
		["list_users"]=> bool(true)
		["remove_users"]=> bool(true)
		["promote_users"]=> bool(true)
		["edit_theme_options"]=> bool(true)
		["delete_themes"]=> bool(true)
		["export"]=> bool(true)
		["administrator"]=> bool(true)
	}
	["filter"]=> NULL
	["site_id":"WP_User":private]=> int(1)

}

ユーザーのメタデータを取得する

上記の方法ではユーザーの基本情報を取得できますが、WordPress管理ページのプロフィールで入力する「名(first_name)」、「姓(last_name)」、「ニックネーム(nickname)」、「プロフィール情報(description)」などの情報はメタデータとして登録されているため取得できません。
メタデータはget_user_meta()にユーザーIDを指定して取得します。

index.php

<?php

$user = wp_get_current_user();
$user_meta = get_user_meta($user->ID);

以下のように登録された情報を配列形式で取得することができます。

取得できるユーザー情報

array(20) {
	["nickname"]=> array(1) {
		[0]=> string(6) "管理者"
	}
	["first_name"]=> array(1) {
		[0]=> string(0) "太郎"
	}
	["last_name"]=> array(1) {
		[0]=> string(0) "山田"
	}
	["description"]=> array(1) {
		[0]=> string(15) "ここに紹介文"
	}
	["rich_editing"]=> array(1) {
		[0]=> string(4) "true"
	}
	["syntax_highlighting"]=> array(1) {
		[0]=> string(4) "true"
	}
	["comment_shortcuts"]=> array(1) {
		[0]=> string(5) "false"
	}
	["admin_color"]=> array(1) {
		[0]=> string(5) "fresh"
	}
	["use_ssl"]=> array(1) {
		[0]=> string(1) "0"
	}
	["show_admin_bar_front"]=> array(1) {
		[0]=> string(4) "true"
	}
	["locale"]=> array(1) {
		[0]=> string(0) ""
	}
	["wp_capabilities"]=> array(1) {
		[0]=> string(31) "a:1:{s:13:"administrator";b:1;}"
	}
	["wp_user_level"]=> array(1) {
		[0]=> string(2) "10"
	}
	["dismissed_wp_pointers"]=> array(1) {
		[0]=> string(0) "" 
	}
	["show_welcome_panel"]=> array(1) {
		[0]=> string(1) "1"
	}
	["wp_user-settings"]=> array(1) {
		[0]=> string(226) "libraryContent=browse&ampampeditor=html& ..省略.. =html&editor=html"
	}
	["wp_user-settings-time"]=> array(1) {
		[0]=> string(10) "1644906087"
	}
	["wp_dashboard_quick_press_last_post_id"]=> array(1) {
		[0]=> string(1) "4"
	}
	["community-events-location"]=> array(1) {
		[0]=> string(33) "a:1:{s:2:"ip";s:10:"172.20.0.0";}"
	}
	["session_tokens"]=> array(1) {
		[0]=> string(305) "a:1:{s:64:"d95f0c9572e9f55be7681926bd9 ..省略.. 903839;}}"
	}
}

こちらの記事は役に立ちましたか?

ありがとうございます。
もしよろしければ、あわせてフィードバックや要望などをご入力ください。

コメントありがとうございます!
運営の参考にさせていただきます。

前のページへ 一覧に戻る 次のページへ