[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;
}
?>

Last updated