当サイト、Codex 日本語版は今後積極的な更新は行わない予定です。後継となる新ユーザーマニュアルは、https://ja.wordpress.org/support/ にあります。
万が一、当サイトで重大な問題を発見した際などは、フォーラムWordSlack #docs チャンネルでお知らせください。</p>

関数リファレンス/add cap

提供: WordPress Codex 日本語版
< 関数リファレンス
2018年8月23日 (木) 12:48時点におけるNorando (トーク | 投稿記録)による版 (en:Function Reference/add cap 08:15, 6 November 2017‎ Tazotodua 版)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

Description

Adds a capability to a role or specific user. Changing the capabilities of a role or user is persistent, meaning the added capability will stay in effect until explicitly revoked.

N.B.: This setting is saved to the database (in table wp_options, field wp_user_roles), so it might be better to run this on theme/plugin activation

Usage

<?php 
 global $wp_roles; // global class wp-includes/capabilities.php
 $wp_roles->add_cap( $role, $cap ); 
?> 
or
<?php
 $role = get_role( 'author' );
 $role->add_cap( $cap );
?>

Parameters

role (just with WP_Roles)
string) (Required) role name
初期値: なし
cap
string) (Required) capability name
初期値: なし
grant
boolean) (optional) Whether the role is capable of performing this capability.
初期値: true

Return Values

void 
No return value

Example

function add_theme_caps() {
    // gets the author role
    $role = get_role( 'author' );

    // This only works, because it accesses the class instance.
    // would allow the author to edit others' posts for current theme only
    $role->add_cap( 'edit_others_posts' ); 
}
add_action( 'admin_init', 'add_theme_caps');

NB: This setting is saved to the database, so it might be better to run this on theme/plugin activation

function add_theme_caps(){
  global $pagenow;

  // gets the author role
  $role = get_role( 'author' );

  if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) ){ // Test if theme is activated
    // Theme is activated

    // This only works, because it accesses the class instance.
    // would allow the author to edit others' posts for current theme only
    $role->add_cap( 'edit_others_posts' ); 
  }
  else {
    // Theme is deactivated
    // Remove the capability when theme is deactivated
    $role->remove_cap( 'edit_others_posts' ); 
  }
}
add_action( 'load-themes.php', 'add_theme_caps' );

To add capability to specific user :

$user = new WP_User( $user_id );
$user->add_cap( 'can_edit_posts' );

Notes

There is no public function called add_cap() - just the class methods inside WP_Roles, WP_Role, WP_User that can add capabilities.

If you want to add a new role with capabilities, just add them when you add the role using add_role();.

Changelog

Source File

add_cap is located in three places:

wp-includes/class-wp-role.php, wp-includes/class-wp-roles.php, wp-includes/class-wp-user.php

Related

権限グループと権限:


最新英語版: WordPress Codex » Function Reference/add cap最新版との差分


関数リファレンステンプレートタグ目次もご覧ください。