Skip to content

Instantly share code, notes, and snippets.

@chwnam
Created August 8, 2020 06:48
Show Gist options
  • Save chwnam/9f2dbcbcb2f72b4f96d8890eb25b4842 to your computer and use it in GitHub Desktop.
Save chwnam/9f2dbcbcb2f72b4f96d8890eb25b4842 to your computer and use it in GitHub Desktop.
워드프레스 커스텀 포스트와 커스텀 택소노미의 모든 파라미터를 디테일하게 설정한 코드. 활성화, 비활성화시 관리자에게 권한까지 충분히 고려합니다.
<?php
/**
* Plugin Name: cpt-study
* Description: 커스텀 포스트 스터디 플러그인. 역할, 다시쓰기 등 세부 파라미터를 빠짐없이 디테일하게 정리한 코드.
*/
/**
* @link https://developer.wordpress.org/reference/functions/register_post_type/
*/
function cpt_study_post_type() {
if ( post_type_exists( 'study' ) ) {
return get_post_type_object( 'study' );
}
$labels = [
'name' => '스터디들',
'singular_name' => '스터디',
'add_new' => '새로 추가',
'add_new_item' => '새 스터디 추가',
'edit_item' => '스터디 수정',
'new_item' => '새 스터디',
'view_item' => '스터디 보기',
'view_items' => '스터디들 보기',
'search_items' => '스터디들 검색',
'not_found' => '스터디 검색되지 않음.',
'not_found_in_trash' => '휴지통에서 스터디 검색되지 않음.',
'parent_item_colon' => '상위 스터디:',
'all_items' => '모든 스터디들',
'archives' => '스터디 아카이브',
'attributes' => '스터디 속성들',
'insert_into_item' => '스터디로 삽입',
'uploaded_to_this_item' => '이 스터디로 업로드',
'featured_image' => '대표 이미지',
'set_featured_image' => '대표 이미지로 설정',
'remove_featured_image' => '대표 이미지 제거',
'use_featured_image' => '대표 이미지로 사용',
'filter_items_list' => '스터디 목록 필터',
'items_list_navigation' => '스터디 목록 네비게이션',
'items_list' => '스터디 목록',
'item_published' => '스터디 공개됨.',
'item_published_privately' => '스터디 비공개 발행됨.',
'item_reverted_to_draft' => '스터디 초안으로 돌림.',
'item_scheduled' => '스터디 예약됨.',
'item_updated' => '스터디 수정됨.',
];
$args = [
'label' => '스터디',
'labels' => $labels,
'description' => '스터디 커스텀 포스트.',
'public' => true,
'hierarchical' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'show_in_rest' => true,
'rest_base' => 'study',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'menu_position' => null,
'menu_icon' => 'dashicons-edit',
'capability_type' => [ 'study', 'studies' ],
'capabilities' => [
'edit_post' => 'edit_study',
'read_post' => 'read_study',
'delete_post' => 'delete_study',
'edit_posts' => 'edit_studies',
'edit_others_posts' => 'edit_others_studies',
'delete_posts' => 'delete_studies',
'publish_posts' => 'publish_studies',
'read_private_posts' => 'read_private_studies',
],
'map_meta_cap' => true,
'supports' => [
'title',
'editor',
'comments',
'revisions',
'trackbacks',
'author',
'excerpt',
'page-attributes',
'thumbnail',
'custom-fields',
'post-formats',
],
'register_meta_box_cb' => null,
'taxonomies' => [ 'study_cat' ],
'has_archive' => true,
'rewrite' => [
'slug' => 'study',
'with_front' => true,
'feeds' => true,
'pages' => true,
'ep_mask' => EP_PERMALINK,
],
'query_var' => 'study',
'can_export' => true,
'delete_with_user' => true,
];
$type_obj = register_post_type( 'study', $args );
if ( is_wp_error( $type_obj ) ) {
wp_die( $type_obj );
}
return $type_obj;
}
/**
* @link https://developer.wordpress.org/reference/functions/register_taxonomy/
*/
function cpt_study_taxonomy() {
if ( taxonomy_exists( 'study_cat' ) ) {
return get_taxonomy( 'study_cat' );
}
$args = [
'labels' => [
'name' => '스터디 분류들',
'taxonomy general name',
'singular_name' => '스터디 분류',
'taxonomy singular name',
'search_items' => '스터디 분류 검색',
'popular_items' => '인기 스터디 분류',
'all_items' => '모든 스터디 분류들',
'parent_item' => '상위 스터디 분류',
'parent_item_colon' => '상위 스터디 분류:',
'edit_item' => '스터디 분류 편집',
'view_item' => '스터디 분류 보기',
'update_item' => '스터디 분류 갱신',
'add_new_item' => '새 스터디 분류 추가',
'new_item_name' => '새 스터디 분류 이름',
'separate_items_with_commas' => '콤마로 분류 구분',
'add_or_remove_items' => '분류를 추가 혹은 삭제',
'choose_from_most_used' => '가장 많이 사용된 항목에서 선택',
'not_found' => '스터디 분류 찾지 못함.',
'no_terms' => '스터디 분류 없음N',
'items_list_navigation' => '스터디 분류 목록 네비게이션',
'items_list' => '스터디 분류 목록',
/* translators: Tab heading when selecting from the most used terms. */
'most_used' => '가장 많이 사용된 스터디 분류',
'back_to_items' => '&larr; 목록으로 돌아감',
],
'description' => '스터디를 위한 위계적 택소노미.',
'public' => true,
'publicly_queryable' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'rest_base' => 'study-cat',
'rest_controller_class' => 'WP_REST_Terms_Controller',
'show_tagcloud' => true,
'show_in_quick_edit' => true,
'show_admin_column' => true,
'meta_box_cb' => null,
'meta_box_sanitize_cb' => null,
'capabilities' => [
'manage_terms' => 'manage_study_cats',
'edit_terms' => 'edit_study_cats',
'delete_terms' => 'delete_study_cats',
'assign_terms' => 'assign_study_cats',
],
'rewrite' => [
'slug' => 'study-cat',
'with_front' => true,
'hierarchical' => false,
'ep_pask' => EP_NONE,
],
'query_var' => 'study-cat',
'sort' => false,
'update_count_callback' => null
];
$taxonomy = register_taxonomy( 'study_cat', [ 'study' ], $args );
if ( is_wp_error( $taxonomy ) ) {
wp_die( $taxonomy );
}
return $taxonomy;
}
function cpt_study_grant_admins() {
$post_obj = cpt_study_post_type();
$taxonomy = cpt_study_taxonomy();
$roles = wp_roles();
if ( $roles && $post_obj ) {
$caps = [
$post_obj->cap->edit_posts,
$post_obj->cap->edit_others_posts,
$post_obj->cap->delete_posts,
$post_obj->cap->publish_posts,
$post_obj->cap->read_private_posts,
$post_obj->cap->delete_private_posts,
$post_obj->cap->delete_published_posts,
$post_obj->cap->delete_others_posts,
$post_obj->cap->edit_private_posts,
$post_obj->cap->edit_published_posts,
$taxonomy->cap->assign_terms,
$taxonomy->cap->delete_terms,
$taxonomy->cap->edit_terms,
$taxonomy->cap->manage_terms,
];
foreach ( $caps as $cap ) {
$roles->roles['administrator']['capabilities'][ $cap ] = true;
}
update_option( $roles->role_key, $roles->roles );
}
}
function cpt_study_revoke_admins() {
$post_obj = cpt_study_post_type();
$taxonomy = cpt_study_taxonomy();
$roles = wp_roles();
if ( $roles && $post_obj ) {
$caps = [
$post_obj->cap->delete_others_posts,
$post_obj->cap->delete_posts,
$post_obj->cap->delete_private_posts,
$post_obj->cap->delete_published_posts,
$post_obj->cap->edit_others_posts,
$post_obj->cap->edit_posts,
$post_obj->cap->edit_private_posts,
$post_obj->cap->edit_published_posts,
$post_obj->cap->publish_posts,
$post_obj->cap->read_private_posts,
$taxonomy->cap->assign_terms,
$taxonomy->cap->delete_terms,
$taxonomy->cap->edit_terms,
$taxonomy->cap->manage_terms,
];
foreach ( $caps as $cap ) {
unset( $roles->roles['administrator']['capabilities'][ $cap ] );
}
update_option( $roles->role_key, $roles->roles );
}
}
function cpt_study_init() {
cpt_study_post_type();
cpt_study_taxonomy();
}
add_action( 'init', 'cpt_study_init', 500 );
register_activation_hook( __FILE__, 'cpt_study_grant_admins' );
register_deactivation_hook( __FILE__, 'cpt_study_revoke_admins' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment