[API] Nếu API công khai, bạn có thể sử dụng __return_true làm lệnh gọi lại (ok)

<?php
add_action('rest_api_init', function () {
register_rest_route('myplugin/v1', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
'permission_callback' => '__return_true',
));
});
function my_awesome_func($data) {
$posts = get_posts(array(
'author' => $data['id'],
));
if (empty($posts)) {
return new WP_Error('no_author', 'Invalid author', array('status' => 404));
}
return $posts[0]->post_title;
}
?>
Previous[API] Adding Custom EndpointsNext[API] Một ví dụ nghiên cứu register_rest_route callback && permission_callback (ok)
Last updated
Was this helpful?