WordPress: 커스텀 투고 타입용 기능
커스텀 post_type을 작성하는 플러그인을 쓰고 있습니다.또한 새로운 post_type만 추가/편집/삭제할 수 있는 커스텀 롤을 플러그인에서 생성해 주셨으면 합니다.몇 가지 플러그인(Role Scoper, Advanced Access Manager)을 사용해 봤는데, 이 플러그인에서는 새로운 post_type에 고유한 기능을 할당할 수 없습니다.예를 들어 일반 투고/페이지가 아닌 새로운 post_type을 추가/편집할 수 있도록 합니다.
제가 읽은 바로는 add_role() 함수로 새로운 역할을 추가할 수 있습니다.이 함수의 파라미터 중 하나는 여기에 정의되어 있는 것처럼 보이는 "기능" 배열입니다.필요한 것은 MY post_type 고유의 기능을 추가할 수 있는 것입니다.이게 가능합니까?
커스텀 포스트 타입의 기능
이 함수는$capabilities
array를 인수(옵션)의 배열을 지정합니다.
다음과 같이 보일 수 있습니다.
$capabilities = array(
'publish_posts' => 'publish_ypts',
'edit_posts' => 'edit_ypts',
'edit_others_posts' => 'edit_others_ypts',
'delete_posts' => 'delete_ypts',
'delete_others_posts' => 'delete_others_ypts',
'read_private_posts' => 'read_private_ypts',
'edit_post' => 'edit_ypt',
'delete_post' => 'delete_ypt',
'read_post' => 'read_ypt'
);
여기서 "ypt"는 "your post type"을 나타냅니다.
그런 다음 WordPress에 다음과 같은 정확한 기능(및 일부 표준 WordPress 기능)을 갖춘 새로운 역할을 추가할 수 있습니다.
add_role(
'ypt_author',
'Author of your post type',
array(
'publish_ypts' => true,
'edit_ypts' => true,
'edit_others_ypts' => true,
'delete_ypts' => true,
'delete_others_ypts' => true,
'read_private_ypts' => true,
'edit_ypt' => true,
'delete_ypt' => true,
'read_ypt' => true,
// more standard capabilities here
)
);
후자는 플러그인을 사용하여 수행할 수 있습니다. 예를 들어 Justin Tadlock의 Members 플러그인을 확인하십시오.
완전한 예
좀 더 구체적인 예를 들자면:
/* REGISTER POST TYPE */
add_action('init', 'ypt_register');
function ypt_register()
{
$labels = array(
'name' => _x('YPTs', 'post type general name'),
'singular_name' => _x('YPT', 'post type singular name'),
'add_new' => _x('Add New YPT', 'Team item'),
'add_new_item' => __('Add a new post of type YPT'),
'edit_item' => __('Edit YPT'),
'new_item' => __('New YPT'),
'view_item' => __('View YPT'),
'search_items' => __('Search YPTs'),
'not_found' => __('No YPTs found'),
'not_found_in_trash' => __('No YPTs currently trashed'),
'parent_item_colon' => ''
);
$capabilities = array(
// this is where the first code block from above goes
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'ypt',
'capabilities' => $capabilities,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'author', 'thumbnail' )
);
register_post_type( 'ypt' , $args );
flush_rewrite_rules( false );
}
/* MAP META CAPABILITIES */
add_filter( 'map_meta_cap', 'ypt_map_meta_cap', 10, 4 );
function ypt_map_meta_cap( $caps, $cap, $user_id, $args )
{
if ( 'edit_ypt' == $cap || 'delete_ypt' == $cap || 'read_ypt' == $cap ) {
$post = get_post( $args[0] );
$post_type = get_post_type_object( $post->post_type );
$caps = array();
}
if ( 'edit_ypt' == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->edit_posts;
else
$caps[] = $post_type->cap->edit_others_posts;
}
elseif ( 'delete_ypt' == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->delete_posts;
else
$caps[] = $post_type->cap->delete_others_posts;
}
elseif ( 'read_ypt' == $cap ) {
if ( 'private' != $post->post_status )
$caps[] = 'read';
elseif ( $user_id == $post->post_author )
$caps[] = 'read';
else
$caps[] = $post_type->cap->read_private_posts;
}
return $caps;
}
요즘은 (WP 3.5+) 훨씬 편해졌어요.설정만 하면 됩니다.map_meta_cap
에 대한 의론.TRUE
를 선택합니다.string
(일반적으로 포스트타입명)의capability_type
post 타입을 등록할 때 인수를 지정합니다.
단순.var_dump( $GLOBALS['wp_post_types']['new_custom_post_type'] ) );
다음과 같은 것을 보여 드리겠습니다.
[cap] => stdClass Object
(
[edit_post] => "edit_{$capability_type}"
[read_post] => "read_{$capability_type}"
[delete_post] => "delete_{$capability_type}"
[edit_posts] => "edit_{$capability_type}s"
[edit_others_posts] => "edit_others_{$capability_type}s"
[publish_posts] => "publish_{$capability_type}s"
[read_private_posts] => "read_private_{$capability_type}s"
[delete_posts] => "delete_{$capability_type}s"
[delete_private_posts] => "delete_private_{$capability_type}s"
[delete_published_posts] => "delete_published_{$capability_type}s"
[delete_others_posts] => "delete_others_{$capability_type}s"
[edit_private_posts] => "edit_private_{$capability_type}s"
[edit_published_posts] => "edit_published_{$capability_type}s"
)
보다 의도된 어레이 부분은 코어별로 체크되지 않고 매핑되는 다른 7가지 기본 기능입니다.map_meta_caps()
post type 등록 중.
언급URL : https://stackoverflow.com/questions/8198038/wordpress-capabilities-for-custom-post-types
'programing' 카테고리의 다른 글
php 프로세스 메모리 사용을 최적화하는 방법 (0) | 2023.03.02 |
---|---|
리액트 라우터에서 링크 컴포넌트를 추가하는 Material-ui (0) | 2023.03.02 |
javascript에서 json 개체를 정렬합니다. (0) | 2023.02.25 |
Model, ModelMap 및 ModelAndView의 차이점은 무엇입니까? (0) | 2023.02.25 |
Spring Boot 디폴트테스트에서 Ilgulate State Exception이 느려집니다. (0) | 2023.02.25 |