投稿日:2020.7.27 更新日:2021.1.16(権限グループ名と権限名について追記・応用編修正)
カスタム投稿タイプの記事のみ扱えるユーザーを作る。
※プラグイン「User Role Editor」を使うことが前提条件
1. User Role Editorで権限グループを追加する
2. 権限を追加する(この権限名とfunction.phpに記述する権限名を合わせる)
function.phpにカスタム投稿タイプ用の権限を用意する
「authid」というidの権限グループと「ptauth」というカスタム投稿タイプ用の各権限を新たに追加し、管理者(administrator)にも追加するというもの。
function my_custom_post_type() {
register_post_type(
'ptname',
array(
'label' => 'カスタム投稿タイプのスラッグ名',
'public' => true,
'capability_type' => 'ptauth',
'has_archive' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields' ,'comments' )
)
);
$rm = new WP_Roles();
$rm->add_role('authid', '権限名' );
foreach( array( 'authid', 'administrator' ) as $rid ) {
$role = $rm->get_role($rid);
$role->add_cap('read');
$role->add_cap('add_ptauth');
$role->add_cap('add_ptauths');
$role->add_cap('edit_ptauth');
$role->add_cap('edit_ptauths');
$role->add_cap('delete_ptauth');
$role->add_cap('delete_ptauths');
$role->add_cap('publish_ptauths');
}
$role->add_cap('delete_others_ptauths');
$role->add_cap('edit_others_ptauths');
}
add_action( 'init', 'my_custom_post_type', 0 );
add_roleについてWordPress Codexの関数リファレンスより。
「wordPressに新しい権限グループを追加します」
<?php add_role( $role, $display_name, $capabilities ); ?>
$role = 権限グループ名 $display_name = 権限グループ表示名 $capabilities = 権限の配列
上記のコードでは「authid」が権限グループ名ということになり、「権限名」は権限グループ表示名になる
応用編に続く
(続きを読む…)