はじめに
WordPress の users
テーブルは、ユーザーに関する重要な情報のみを格納するように設計されています。
ID
、user_login
、user_pass
、user_nicename
、user_email
、user_url
、user_registered
、user_activation_key
、user_status
、display_name
。
このため、ユーザーに関するあらゆる種類のデータを保存できる追加データを保存するために、usermeta
テーブルが導入されました。
両テーブルは users
テーブルの ID
をもとにした一対多のリレーションシップで結ばれています。
ユーザーメタデータの操作
ユーザーメタデータを操作するには、主に2つの方法があります。
- ユーザーのプロフィール画面のフォームフィールド。
- プログラムによる、関数呼び出し。
フォームフィールド経由
フォームフィールドオプションは、ユーザーが WordPress の管理エリアにアクセスでき、そこでプロフィールを閲覧・編集できる場合に適しています。
例を取り上げる前に、そのプロセスに関わるフックとその理由を理解することが重要です。
show_user_profile
フック
このアクションフックは、ユーザーが 自分の ユーザープロファイルを編集するたびに発生します。
自分のプロフィールを編集する権限を持っていないユーザーは、このフックを使うことができないということを、覚えておいてください。
edit_user_profile
フック
このアクションフックは、ユーザーが誰かのユーザープロファイルを編集するたびに発生します。
サードパーティのプロフィールを編集する権限を持っていないユーザーは、このフックを使うことができないということを、覚えておいてください。
フォーム・フィールドの例
以下の例では、すべてのプロフィール画面に誕生日フィールドを追加します。プロフィールの更新時にそれをデータベースに保存します。
/**
* The field on the editing screens.
*
* @param $user WP_User user object
*/
function wporg_usermeta_form_field_birthday( $user ) {
?>
<h3>It's Your Birthday</h3>
<table class="form-table">
<tr>
<th>
<label for="birthday">Birthday</label>
</th>
<td>
<input type="date"
class="regular-text ltr"
id="birthday"
name="birthday"
value="<?= esc_attr( get_user_meta( $user->ID, 'birthday', true ) ) ?>"
title="Please use YYYY-MM-DD as the date format."
pattern="(19[0-9][0-9]|20[0-9][0-9])-(1[0-2]|0[1-9])-(3[01]|[21][0-9]|0[1-9])"
required>
<p class="description">
Please enter your birthday date.
</p>
</td>
</tr>
</table>
<?php
}
/**
* The save action.
*
* @param $user_id int the ID of the current user.
*
* @return bool Meta ID if the key didn't exist, true on successful update, false on failure.
*/
function wporg_usermeta_form_field_birthday_update( $user_id ) {
// check that the current user have the capability to edit the $user_id
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
// create/update user meta for the $user_id
return update_user_meta(
$user_id,
'birthday',
$_POST['birthday']
);
}
// Add the field to user's own profile editing screen.
add_action(
'show_user_profile',
'wporg_usermeta_form_field_birthday'
);
// Add the field to user profile editing screen.
add_action(
'edit_user_profile',
'wporg_usermeta_form_field_birthday'
);
// Add the save action to user's own profile editing screen update.
add_action(
'personal_options_update',
'wporg_usermeta_form_field_birthday_update'
);
// Add the save action to user profile editing screen update.
add_action(
'edit_user_profile_update',
'wporg_usermeta_form_field_birthday_update'
);
プログラム的に
このオプションは、カスタムユーザーエリアを構築している場合、および/または、WordPress 管理エリアへのアクセスを無効にする予定がある場合に適しています。
ユーザーメタデータを操作するために利用できる関数は以下の通りです: add_user_meta()
、update_user_meta()
、delete_user_meta()
、get_user_meta()
。
追加
add_user_meta(
int $user_id,
string $meta_key,
mixed $meta_value,
bool $unique = false
);
使用パラメータについての完全な説明は、add_user_meta()
に関する関数リファレンスを参照してください。
更新
update_user_meta(
int $user_id,
string $meta_key,
mixed $meta_value,
mixed $prev_value = ''
);
使用パラメータについての完全な説明は、update_user_meta()
に関する関数リファレンスを参照してください。
削除
delete_user_meta(
int $user_id,
string $meta_key,
mixed $meta_value = ''
);
使用パラメータについての完全な説明は、delete_user_meta()
に関する関数リファレンスを参照してください。
取得
get_user_meta(
int $user_id,
string $key = '',
bool $single = false
);
使用パラメータについての完全な説明は、get_user_meta()
に関する関数リファレンスを参照してください。
$user_id
のみを渡すと、この関数はすべてのメタデータを連想配列として取得することに注意してください。
ユーザーメタデータは、プラグインやテーマのどこにでも表示できます。