Created
October 4, 2012 06:52
-
-
Save gatespace/3831881 to your computer and use it in GitHub Desktop.
カスタム投稿タイプと専用ユーザー権限
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: カスタム投稿タイプと専用ユーザー権限 | |
Plugin URI: | |
Description: カスタム投稿タイプ「report」とカスタムタクソノミー「reportcat」を作成し、専用の権限グループ「レポート投稿者(report_author)」を作成します。 | |
Author: Your Name | |
Version: 1.0 | |
Author URI: | |
*/ | |
function codex_custom_init() { | |
/** | |
* カスタムタクソノミー reportcat | |
*/ | |
$reportcat_labels = array( | |
'name' => "レポートカテゴリ", | |
'singular_name' => "レポートカテゴリ", | |
'search_items' => "カテゴリを検索", | |
'all_items' => "すべてのカテゴリ", | |
'parent_item' => "親カテゴリ", | |
'parent_item_colon' => "親:", | |
'edit_item' => "カテゴリを編集", | |
'update_item' => "カテゴリを更新", | |
'add_new_item' => "新規カテゴリ", | |
'new_item_name' => "新規カテゴリ名", | |
'menu_name' => "レポートカテゴリ", | |
); | |
register_taxonomy( 'reportcat', array( 'report' ), array( | |
'hierarchical' => true, | |
'labels' => $reportcat_labels, | |
'show_ui' => true, | |
'query_var' => true, | |
'rewrite' => true, | |
'capabilities' => array( 'assign_terms' => 'edit_post_reports' ) | |
)); | |
/** | |
* カスタム投稿タイプ report | |
*/ | |
$report_labels = array( | |
'name' => "レポート", | |
'singular_name' => "レポート", | |
'add_new' => "新規追加", | |
'add_new_item' => "新規レポート追加", | |
'edit_item' => "レポートを編集", | |
'new_item' => "新規レポート", | |
'view_item' => "表示", | |
'search_items' => "レポートを検索", | |
'not_found' => "見つかりません", | |
'not_found_in_trash' =>"ゴミ箱にはありません", | |
'parent_item_colon' => '親', | |
'menu_name' => 'レポート' | |
); | |
$report_args = array( | |
'labels' => $report_labels, | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'rewrite' => true, | |
'capability_type' => 'post_report', // 通常はpostだが任意のpost_reportにする | |
'has_archive' => true, | |
'hierarchical' => false, | |
'menu_position' => "5", | |
'map_meta_cap' => true, | |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ), | |
'taxonomies' => array( 'reportcat' ) | |
); | |
register_post_type( 'report', $report_args ); | |
/** | |
* post_report にかかわる権限の付与 | |
*/ | |
// 新しい権限グループ「report_author」の作成 | |
add_role( 'report_author', 'レポート投稿者' , array( 'read' => true ) ); | |
// 権限グループをすべて取得 | |
$roles = new WP_Roles(); | |
// 付与すべき権限をすべて取得 | |
$post_report_caps = $GLOBALS['wp_post_types']['report']->cap; | |
foreach ($roles->roles as $key => $value ) { | |
if ( $key == "subscriber" ) continue; // 購読者はスキップ | |
$role = get_role($key); | |
if ( $key == "report_author" ) { // レポート投稿者の時 | |
$role->add_cap( "upload_files" ); // ファイルのアップロード権限付与 | |
} | |
foreach ($post_report_caps as $post_report_cap) { | |
if ( $post_report_cap == "read" ) continue; // read は既に登録されているのでスキップ | |
if ( $key == "contributor" ) { // 投稿者に必要のない権限はスキップ | |
if ( $post_report_cap == "delete_others_post_reports" ) continue; | |
if ( $post_report_cap == "delete_private_post_reports" ) continue; | |
if ( $post_report_cap == "edit_others_post_reports" ) continue; | |
if ( $post_report_cap == "edit_private_post_reports" ) continue; | |
} elseif ( $key == "author" ) { // 寄稿者に必要のない権限はスキップ | |
if ( $post_report_cap == "delete_others_post_reports" ) continue; | |
if ( $post_report_cap == "delete_private_post_reports" ) continue; | |
if ( $post_report_cap == "delete_published_post_reports" ) continue; | |
if ( $post_report_cap == "edit_others_post_reports" ) continue; | |
if ( $post_report_cap == "edit_private_post_reports" ) continue; | |
if ( $post_report_cap == "edit_published_post_reports" ) continue; | |
if ( $post_report_cap == "publish_post_reports" ) continue; | |
} elseif ( $key == "report_author" ) { // レポート投稿者に必要のない権限はスキップ | |
if ( $post_report_cap == "delete_others_post_reports" ) continue; | |
if ( $post_report_cap == "delete_private_post_reports" ) continue; | |
if ( $post_report_cap == "edit_others_post_reports" ) continue; | |
if ( $post_report_cap == "edit_private_post_reports" ) continue; | |
} | |
$role->add_cap( $post_report_cap ); | |
} | |
} | |
} | |
add_action( 'init', 'codex_custom_init', 0); | |
/** | |
* report_author のログイン後のリダイレクト先を変更 | |
*/ | |
function my_login_redirect( $redirect_to, $request, $user ) { | |
if (in_array("report_author", $user->roles)) { | |
return home_url("/wp-admin/index.php"); | |
} else { | |
return home_url("/wp-admin/"); | |
} | |
} | |
add_filter("login_redirect", "my_login_redirect", 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment