- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
「関数リファレンス/add role」の版間の差分
(en:Function_Reference/add_role 18:20, 12 June 2014 Alexander.rohmann 版) |
(参考節以外を和訳) |
||
1行目: | 1行目: | ||
− | + | {{NeedTrans|[[#Notes|参考]]節が}} | |
− | + | <div id="Description"> | |
+ | == 説明 == | ||
+ | </div> | ||
− | + | WordPressに新しい[[ユーザーの種類と権限#Roles|権限グループ]]を追加します。 | |
− | + | '''注意: この設定はデータベース(wp_optionsテーブルのwp_user_rolesフィールド)に保存されるので、テーマやプラグインの有効化の時に実行するのが良いでしょう。''' | |
− | + | <div id="Usage"> | |
+ | == 使い方 == | ||
+ | </div> | ||
− | + | <?php add_role( $role, $display_name, $capabilities ); ?> | |
− | + | <div id="Parameters"> | |
− | + | == パラメータ == | |
− | + | </div> | |
− | + | {{Parameter|$role|文字列|権限グループ名|必須}} | |
+ | {{Parameter|$display_name|文字列|権限グループ表示名|必須}} | ||
+ | {{Parameter|$capabilities|配列|権限の配列(使用可能な機能のリストは[[ユーザーの種類と権限|ユーザーの種類と権限]]を参照}} | ||
− | + | <div id="Return Values"> | |
+ | == 戻り値 == | ||
+ | </div> | ||
− | + | ; (mixed) : 成功時はWP_Roleオブジェクトと返し、その権限グループがすでに存在する場合Nullを返します。 | |
− | + | <div id="Examples"> | |
+ | == 例 == | ||
+ | </div> | ||
+ | |||
+ | 新しく"Basic Contributor"権限グループを作成します。 | ||
$result = add_role( | $result = add_role( | ||
27行目: | 39行目: | ||
__( 'Basic Contributor' ), | __( 'Basic Contributor' ), | ||
array( | array( | ||
− | 'read' => true, // | + | 'read' => true, // trueはこの機能を使用可にします。 |
'edit_posts' => true, | 'edit_posts' => true, | ||
− | 'delete_posts' => false, // | + | 'delete_posts' => false, // 明示的に使用しないとする場合はfalseを使用します。 |
) | ) | ||
); | ); | ||
if ( null !== $result ) { | if ( null !== $result ) { | ||
− | echo ' | + | echo '新しい権限グループを作成しました!'; |
} | } | ||
else { | else { | ||
− | echo ' | + | echo 'すでに"basic_contributor"権限グループは存在します。'; |
} | } | ||
− | + | プラグインを有効化した際に新しい権限グループを作成します。参照:[[関数リファレンス/register_activation_hook|register_activation_hook]] | |
function add_roles_on_plugin_activation() { | function add_roles_on_plugin_activation() { | ||
46行目: | 58行目: | ||
register_activation_hook( __FILE__, 'add_roles_on_plugin_activation' ); | register_activation_hook( __FILE__, 'add_roles_on_plugin_activation' ); | ||
− | = | + | <div id="Notes"> |
+ | == 参考 == | ||
+ | </div> | ||
− | ===When to | + | ===When to cal=== |
Make sure that the global <tt>$wp_roles</tt> is available before attempting to add or modify a role. The best practice of course is to use your plugin- or theme-activation hook to make changes to roles (since you only want to do it once!). | Make sure that the global <tt>$wp_roles</tt> is available before attempting to add or modify a role. The best practice of course is to use your plugin- or theme-activation hook to make changes to roles (since you only want to do it once!). | ||
62行目: | 76行目: | ||
'''This is for development only. Once you have nailed down your list of capabilities, there's no need to keep the <code>remove_role()</code> code''', though there is, in fact, no harm in doing so. | '''This is for development only. Once you have nailed down your list of capabilities, there's no need to keep the <code>remove_role()</code> code''', though there is, in fact, no harm in doing so. | ||
− | == | + | <div id="Change Log"> |
− | + | == 変更履歴 == | |
− | + | </div> | |
+ | バージョン [[Version 2.0|2.0.0]] にて導入されました。 | ||
− | = | + | <div id="Source File"> |
+ | == ソースファイル == | ||
+ | </div> | ||
− | < | + | <tt>add_role()</tt> は {{Trac|wp-includes/capabilities.php}} にあります。 |
− | = | + | <div id="Related"> |
+ | == 関連項目 == | ||
+ | </div> | ||
{{Role and Capability Tags}} | {{Role and Capability Tags}} |
2015年3月9日 (月) 17:05時点における版
このページ「関数リファレンス/add role」は参考節が未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています。
目次
説明
WordPressに新しい権限グループを追加します。
注意: この設定はデータベース(wp_optionsテーブルのwp_user_rolesフィールド)に保存されるので、テーマやプラグインの有効化の時に実行するのが良いでしょう。
使い方
<?php add_role( $role, $display_name, $capabilities ); ?>
パラメータ
- $role
- (文字列) (必須) 権限グループ名
- 初期値: なし
- $display_name
- (文字列) (必須) 権限グループ表示名
- 初期値: なし
- $capabilities
- (配列) (必須) 権限の配列(使用可能な機能のリストはユーザーの種類と権限を参照
- 初期値: なし
戻り値
- (mixed)
- 成功時はWP_Roleオブジェクトと返し、その権限グループがすでに存在する場合Nullを返します。
例
新しく"Basic Contributor"権限グループを作成します。
$result = add_role( 'basic_contributor', __( 'Basic Contributor' ), array( 'read' => true, // trueはこの機能を使用可にします。 'edit_posts' => true, 'delete_posts' => false, // 明示的に使用しないとする場合はfalseを使用します。 ) ); if ( null !== $result ) { echo '新しい権限グループを作成しました!'; } else { echo 'すでに"basic_contributor"権限グループは存在します。'; }
プラグインを有効化した際に新しい権限グループを作成します。参照:register_activation_hook
function add_roles_on_plugin_activation() { add_role( 'custom_role', 'Custom Subscriber', array( 'read' => true, 'level_0' => true ) ); } register_activation_hook( __FILE__, 'add_roles_on_plugin_activation' );
参考
When to cal
Make sure that the global $wp_roles is available before attempting to add or modify a role. The best practice of course is to use your plugin- or theme-activation hook to make changes to roles (since you only want to do it once!).
mu-plugins will load too early, so use an action hook (like 'init') to wrap your add_role()
call if you're doing this in the context of an mu-plugin.
Delete existing role
If you are defining a custom role, and adding capabilities to the role using add_role()
, be aware that modifying the capabilities array and re-executing add_role()
will not necessarily update the role with the new capabilities list. The add_role()
function short-circuits if the role already exists in the database.
The workaround in this case is to precede your add_role()
call with a remove_role()
call that targets the role you are adding.
This is for development only. Once you have nailed down your list of capabilities, there's no need to keep the remove_role()
code, though there is, in fact, no harm in doing so.
変更履歴
バージョン 2.0.0 にて導入されました。
ソースファイル
add_role() は wp-includes/capabilities.php
にあります。
関連項目
- add_role()
- remove_role()
- get_role()
- add_cap() /en
- remove_cap() /en
最新英語版: WordPress Codex » Function_Reference/add_role (最新版との差分)