• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

クラスリファレンス/WP User

提供: WordPress Codex 日本語版
< クラスリファレンス
2015年5月27日 (水) 07:19時点におけるMiccweb (トーク | 投稿記録)による版 (関連資料: link)

移動先: 案内検索

このページ「クラスリファレンス/WP User」は一部未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

説明

WordPress User クラスを使うと、特定のユーザーの属性・権限グループ・権限にアクセスできます。

使い方

特定のユーザーのインスタンスを作成するには、クラスコンストラクターを使えます。

<?php $users = new WP_User( $id [, $name [, $blog_id ] ] ); ?>

また、wp_get_current_user グローバル関数を使って現在ログインしているユーザーと一致する WP_User オブジェクトを取得することもできます。

<?php $current_user = wp_get_current_user(); ?>

コンストラクター

WP_User コンストラクターでは以下のパラメータが使えます。

  • id (int) - the user's id. Leave empty to use login name instead.
  • name (string) - the user's login name. Ignored if id is set.
  • blog_id (int) - the blog id on a multisite environment. Defaults to the current blog id.

The semantics seem rather fuzzy. If id is a WP_User or other object, or an array of properties, it seems to clone it without touching the cache. Otherwise if id is a number it seems to try to read it possibly from the cache, or from the database using ->get_data_by(), which can also update the cache. If id is empty it reads by login name.

Public Properties

  • ID (int) - the user's ID.
  • caps (array) - the individual capabilities the user has been given.
  • cap_key (string) -
  • roles (array) - the roles the user is part of.
  • allcaps (array) - all capabilities the user has, including individual and role based.
  • first_name (string) - first name of the user.
  • last_name (string) - last name of the user.

Note: If called with the "id" or "name" parameter, the constructor queries the wp_users table. If successful, the additional row data become properties of the object: user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_key, user_status, display_name, spam (multisite only), deleted (multisite only).

Magic Methods

These are PHP5 magic methods that facilitate property overloading. They don't need to be called directly, but are called automatically when a class property is referenced.

__get( $key )

Magic method for accessing custom fields.

Parameters 
  • key (string) - property.
Returns 
(string) Single value of the property filtered.
Since 
3.3.0

__isset( $key )

Determine whether a property or meta key is set from the users and usermeta tables.

Parameters 
  • key (string) - property.
Returns 
(bool) true if user has property, false otherwise.
Since 
3.3.0

__set( $key, $value )

Magic method for setting custom fields.

Please note that setting custom fields affects only the current WP_User object, and does not set the value in the database. To change the database value use wp_update_user() or update_user_meta().

Parameters 
  • key (string) - property.
  • value (string) - value.
Returns 
Nothing.
Since 
3.3.0

Public Methods

exists()

Determine whether the user exists in the database.

It actually tests !empty(ID), which will normally indicate that the user record was in the database at some stage. It does not access the database.

Parameters 
None
Returns 
(bool) true if user exists, false otherwise.
Since 
3.4.0

get( $key )

Retrieve the value of a property or meta key from the users and usermeta tables.

Parameters 
  • key (string) - property.
Returns 
(mixed) String of the property filtered if single value, or array if value is stored as a serialized array
Since 
3.3.0

has_prop( $key )

Determine whether a property or meta key is set from the users and usermeta tables.

Parameters 
  • key (string) - property.
Returns 
(bool) true if user has property, false otherwise.
Since 
3.3.0

get_role_caps()

Retrieve all of the role capabilities and merge with individual capabilities.

All of the capabilities of the roles the user belongs to are merged with the users individual roles. This also means that the user can be denied specific roles that their role might have, but the specific user isn't granted permission to.

Sets the roles and allcaps properties.

Parameters 
None
Returns 
Nothing
Since 
2.0.0

add_role( $role )

Add role to user.

Updates the user's meta data option with capabilities and roles.

Parameters 
  • role (string) - role name.
Returns 
Nothing
Since 
2.0.0

remove_role($role)

Remove role from user.

Parameters 
  • role (string) - role name.
Returns 
Nothing
Since 
2.0.0

set_role($role)

Set the role of the user.

This will remove the previous roles of the user and assign the user the new one. You can set the role to an empty string and it will remove all of the roles from the user.

Parameters 
  • role (string) - role name.
Returns 
Nothing
Since 
2.0.0
Related actions 
* set_user_role

add_cap($cap [, $grant ] )

Add capability and grant or deny access to capability.

Parameters 
  • cap (string) - capability name.
  • grant (bool) - whether to grant capability to user. Default to true.
Returns 
Nothing
Since 
2.0.0

remove_cap($cap)

Remove capability from user.

Parameters 
  • cap (string) - capability name.
Returns 
Nothing
Since 
2.0.0

remove_all_caps()

Remove all of the capabilities of the user.

Parameters 
None
Returns 
Nothing
Since 
2.1.0

has_cap($cap)

Whether user has capability or role name.

Parameters 
  • cap (string) - capability or role name to search.
Returns 
(bool) true if user has capability, false if they do not have the capability.
Since 
2.0.0

用例

Checking if the current user can edit user profiles :

<?php
    $cu = wp_get_current_user();
    if ($cu->has_cap('edit_users')) {
       // do something
    }    
?>

Check if user exists:

<?php
    $user = wp_get_current_user();
    if ( $user->exists() ) {
       // do something
    }    
?>

Check if user has a property:

<?php
    $user = wp_get_current_user();
    if ( $user->has_prop( 'twitter' ) ) {
       // do something
    }    
?>

更新履歴

2.0.0 以降

ソースファイル

WP_Userwp-includes/capabilities.php にあります。

関連資料

外部リンク

クラスリファレンス関数リファレンスの各インデックスも参照してください。

最新英語版: WordPress Codex » Class_Reference/WP_User最新版との差分