考虑以下类别。
<?php
class MCQAcademy_Endpoint extends WP_REST_Controller {
/**
* Register the routes for the objects of the controller.
*/
public function register_routes() {
$version = \'1\';
$namespace = \'custompath/v\' . $version;
$base = \'endpointbase\';
register_rest_route(
$namespace,
\'/\' . $base,
array(
array(
\'methods\' => WP_REST_Server::READABLE,
\'callback\' => array( $this, \'get_items\' ),
\'permission_callback\' => array( $this, \'get_items_permissions_check\' ),
\'args\' => array(),
)
)
);
}
/**
*
*/
public function get_items( $request ) {
$rs = array(
\'data\' => array(),
\'request\' => array(
\'lang\' => \'en\',
),
);
$args = array();
$items = get_posts( $args );
foreach( $items as $item ) {
$itemdata = $this->prepare_item_for_response( $item, $request );
$rs[\'data\'][] = $this->prepare_response_for_collection( $itemdata );
}
$rs[\'wp_get_current_user\'] = wp_get_current_user(); // Does not output as expected
return new WP_REST_Response( $rs, 200 );
}
/**
* Check if a given request has access to get items
*/
public function get_items_permissions_check( $request ) {
return true; // to make readable by all
}
/**
* Prepare the item for create or update operation
*/
protected function prepare_item_for_database( $request ) {
return $request;
}
/**
* Prepare the item for the REST response
*/
public function prepare_item_for_response( $item, $request ) {
$data = array(
\'ID\' => $item->ID,
\'post_content\' => wpautop($item->post_content),
\'post_title\' => $item->post_title,
);
return $data;
}
/**
* Get the query params for collections
*/
public function get_collection_params() {
return array(
\'page\' => array(
\'description\' => \'Current page of the collection.\',
\'type\' => \'integer\',
\'default\' => 1,
\'sanitize_callback\' => \'absint\',
),
\'per_page\' => array(
\'description\' => \'Maximum number of items to be returned in result set.\',
\'type\' => \'integer\',
\'default\' => 10,
\'sanitize_callback\' => \'absint\',
),
\'search\' => array(
\'description\' => \'Limit results to those matching a string.\',
\'type\' => \'string\',
\'sanitize_callback\' => \'sanitize_text_field\',
),
);
}
// Register our REST Server
public function hook_rest_server(){
add_action( \'rest_api_init\', array( $this, \'register_routes\' ) );
}
}
$myEndpoint = new MCQAcademy_Endpoint();
$myEndpoint->hook_rest_server();
除了打电话给
wp_get_current_user()
中的函数
get_items()
函数返回空用户,即使用户是
logged in
在网站上。
SO网友:leymannx
为了获得一个完整的代码示例,这里提供了一个示例插件,介绍如何通过REST检索当前用户的ID。
my-plugin.php
class MyPlugin {
public function __construct() {
add_action(\'wp_enqueue_scripts\', [$this, \'scripts\']);
add_action(\'rest_api_init\', [$this, \'rest\']);
}
function scripts() {
// Add JS.
wp_enqueue_script(\'my-plugin\', plugin_dir_url(__FILE__) . \'js/scripts.js\', [\'jquery\'], NULL, TRUE);
// Pass nonce to JS.
wp_localize_script(\'my-plugin\', \'MyPluginSettings\', [
\'nonce\' => wp_create_nonce(\'wp_rest\'),
]);
}
function rest() {
// Register route.
register_rest_route(\'my-plugin/v1\', \'/uid\', [
\'methods\' => WP_REST_Server::READABLE,
\'callback\' => [$this, \'rest_callback\'],
]);
}
function rest_callback($data) {
// Get current user ID.
$data = [
\'uid\' => get_current_user_id(),
];
$response = new WP_REST_Response($data, 200);
// Set headers.
$response->set_headers([\'Cache-Control\' => \'must-revalidate, no-cache, no-store, private\']);
return $response;
}
}
new MyPlugin();
js/scripts.js
(function($) {
$(document).ready(function() {
var settings = MyPluginSettings;
$.ajax({
url: \'/wp-json/my-plugin/v1/uid\',
method: \'GET\',
beforeSend: function(xhr) {
xhr.setRequestHeader(\'X-WP-Nonce\', settings.nonce);
}
}).done(function(response) {
// Will return your UID.
console.log(response);
});
});
})(jQuery);
资源:
https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/