0
사용자 지정 게시 유형이 card
이며 WP REST API를 통해 노출됩니다.WP REST API 기존 끝점에 대한 사용 권한 설정
function register_card_post_type() {
$labels = array(
"name" => __('Cards', ''),
"singular_name" => __('Card', ''),
);
$args = array(
"label" => __('Cards', ''),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true, // ADD TO REST API
"rest_base" => "cards", // ADD TO REST API
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array("slug" => "card", "with_front" => true),
"query_var" => true,
"menu_position" => 5,
"supports" => array("title"),
);
register_post_type("card", $args);
}
add_action('init', 'register_card_post_type');
기본적으로 끝점은 공개로 보입니다. GET /cards/
에 인증 쿠키 또는 헤더가 필요하도록 끝점에 대한 인증 요구 사항을 어떻게 설정합니까?
API 핸드북에는 맞춤 끝점을 작성하는 방법이 나와 있지만 이상적으로 자동 생성 된 끝점을 확장하는 데 사용할 수있는 필터 또는 후크가 있습니까?
add_filter('rest_pre_dispatch', function() {
$url = strtok($_SERVER["REQUEST_URI"],'?');
if (!is_user_logged_in() &&
!in_array($url, array (//using "in_array" because you can add mmultiple endpoints here
"/wp-json/cards",
))){
return new WP_Error('not-logged-in', 'API Requests to '.$url.' are only supported for authenticated requests', array('status' => 401));
}
});
이이 쿼리를 실행 한 것 때문에 최상의 솔루션되지 않습니다 : 로그인 한 사용자하지 않는
는add_action('rest_api_init', function() {
register_rest_route('myplugin/v1', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
'args' => array(
'id' => array(
'validate_callback' => 'is_numeric'
),
),
'permission_callback' => function() {
return current_user_can('edit_others_posts');
}
));
});