2026年5月20日
2026年5月20日
WordPressのユーザーロールと権限をカスタマイズする方法
はじめに
WordPressには管理者・編集者・投稿者・寄稿者・購読者の5つのデフォルトロールがあります。add_role でカスタムロールを追加し、add_cap・remove_cap で個別の権限を細かく制御できます。
症状・原因
- 特定のユーザーだけに投稿の編集権限を与えたい
- カスタム投稿タイプの管理権限を分けたい
- デフォルトの「投稿者」に権限を追加したい
- 管理画面のメニューをロール別に制限したい
解決手順
ステップ1:デフォルトのロールと権限を理解する
管理者(administrator):すべての権限
編集者(editor):他ユーザーの投稿も編集可
投稿者(author):自分の投稿のみ
寄稿者(contributor):投稿作成のみ(公開不可)
購読者(subscriber):閲覧のみ
主要な権限(capability):
- edit_posts : 投稿の編集
- publish_posts : 投稿の公開
- delete_posts : 投稿の削除
- manage_categories : カテゴリ管理
- upload_files : メディアのアップロード
- manage_options : 設定ページへのアクセス
- edit_others_posts : 他ユーザーの投稿編集
ステップ2:カスタムロールを作成する
// functions.php
function mytheme_add_custom_roles(): void {
// 既に存在する場合はスキップ
if (get_role('content_manager')) {
return;
}
add_role(
'content_manager', // ロールスラッグ
'コンテンツ管理者', // 表示名
[
// 基本権限
'read' => true,
'edit_posts' => true,
'publish_posts' => true,
'delete_posts' => true,
'delete_published_posts' => true,
// メディア
'upload_files' => true,
// 他のユーザーの投稿も編集可
'edit_others_posts' => true,
'delete_others_posts' => true,
// カテゴリ・タグ管理
'manage_categories' => true,
// 管理設定は不可
'manage_options' => false,
]
);
}
add_action('init', 'mytheme_add_custom_roles');
不要になったロールの削除:
// functions.php(一度だけ実行すれば良い)
function mytheme_remove_custom_roles(): void {
remove_role('content_manager');
}
// add_action('init', 'mytheme_remove_custom_roles');
ステップ3:既存ロールに権限を追加・削除する
// functions.php
function mytheme_modify_editor_capabilities(): void {
$editor = get_role('editor');
if (!$editor) {
return;
}
// 編集者にプラグイン管理権限を追加
$editor->add_cap('install_plugins');
$editor->add_cap('activate_plugins');
// 編集者からユーザー管理権限を削除
$editor->remove_cap('create_users');
$editor->remove_cap('delete_users');
}
add_action('init', 'mytheme_modify_editor_capabilities');
ステップ4:カスタム投稿タイプの権限を設定する
// functions.php
function mytheme_register_news_post_type(): void {
register_post_type('news', [
'capability_type' => 'news', // カスタム権限タイプ
'map_meta_cap' => true, // メタ権限を自動マッピング
// 上記で自動生成される権限:
// edit_news, publish_news, delete_news,
// edit_others_news, edit_published_news, etc.
'capabilities' => [
'edit_post' => 'edit_news',
'read_post' => 'read_news',
'delete_post' => 'delete_news',
'edit_posts' => 'edit_newss',
'edit_others_posts' => 'edit_others_newss',
'publish_posts' => 'publish_newss',
],
// ... その他の設定
]);
// content_managerロールにnews権限を付与
$role = get_role('content_manager');
if ($role) {
$role->add_cap('edit_news');
$role->add_cap('edit_newss');
$role->add_cap('publish_newss');
$role->add_cap('delete_news');
$role->add_cap('read_news');
}
}
add_action('init', 'mytheme_register_news_post_type');
ステップ5:管理画面のメニューをロール別に制御する
// functions.php
function mytheme_restrict_admin_menu(): void {
// 管理者以外はツールメニューを非表示
if (!current_user_can('manage_options')) {
remove_menu_page('tools.php');
remove_menu_page('options-general.php');
}
// content_managerには投稿とメディアのみ表示
if (current_user_can('content_manager') && !current_user_can('manage_options')) {
remove_menu_page('edit.php?post_type=page'); // 固定ページを非表示
remove_menu_page('themes.php'); // 外観を非表示
remove_menu_page('plugins.php'); // プラグインを非表示
}
}
add_action('admin_menu', 'mytheme_restrict_admin_menu', 999);
// 管理バーの項目を制御
function mytheme_restrict_admin_bar(WP_Admin_Bar $wp_admin_bar): void {
if (!current_user_can('manage_options')) {
$wp_admin_bar->remove_node('customize');
$wp_admin_bar->remove_node('themes');
}
}
add_action('admin_bar_menu', 'mytheme_restrict_admin_bar', 999);
注意事項
- ロールの権限はデータベース(
wp_optionsテーブルのwp_user_roles)に保存されます。add_cap・remove_capの変更は永続化されるため、一度実行すれば以降は不要です(initフックで毎回実行すると意図しない上書きになる場合があります) remove_menu_pageでメニューを非表示にしても、URLを直接入力するとアクセスできます。権限による制限はcurrent_user_canでサーバーサイドでも行ってくださいadd_roleで追加したカスタムロールは、テーマを変更してもデータベースに残ります。テーマ削除時に不要なロールが残らないよう、switch_themeフックで削除することを検討してください
まとめ
add_role でカスタムロールを作成し、get_role()->add_cap() で個別権限を付与します。カスタム投稿タイプは capability_type + map_meta_cap: true で権限を自動生成し、admin_menu フックでメニューをロール別に制御します。権限変更はDBに永続化されるため init での繰り返し実行に注意してください。