你应该通过特别考试wp_rest
nonce作为请求的一部分。没有它global $current_user
对象在REST类中不可用。您可以通过多种方式传递,从$\\u GET到$\\u POST to headers。
操作nonce是可选的。如果添加了REST端点,则不能从外部服务器使用REST端点,只能从WordPress本身内部调度的请求使用REST端点。用户可以使用Basic Auth, OAuth2, 或JWT 即使没有wp_rest
nonce,但如果同时添加操作nonce,它将无法工作。
因此,操作nonce是可选的。如果希望端点仅在本地工作,请添加它。
示例:
/**
* First step, registering, localizing and enqueueing the JavaScript
*/
wp_register_script( \'main-js\', get_template_directory_uri() . \'/js/main.js\', [ \'jquery\' ] );
wp_localize_script( \'main-js\', \'data\', [
\'rest\' => [
\'endpoints\' => [
\'my_endpoint\' => esc_url_raw( rest_url( \'my_plugin/v1/my_endpoint\' ) ),
],
\'timeout\' => (int) apply_filters( "my_plugin_rest_timeout", 60 ),
\'nonce\' => wp_create_nonce( \'wp_rest\' ),
//\'action_nonce\' => wp_create_nonce( \'action_nonce\' ),
],
] );
wp_enqueue_script( \'main-js\' );
/**
* Second step, the request on the JavaScript file
*/
jQuery(document).on(\'click\', \'#some_element\', function () {
let ajax_data = {
\'some_value\': jQuery( ".some_value" ).val(),
//\'action_nonce\': data.rest.action_nonce
};
jQuery.ajax({
url: data.rest.endpoints.my_endpoint,
method: "GET",
dataType: "json",
timeout: data.rest.timeout,
data: ajax_data,
beforeSend: function (xhr) {
xhr.setRequestHeader(\'X-WP-Nonce\', data.rest.nonce);
}
}).done(function (results) {
console.log(results);
alert("Success!");
}).fail(function (xhr) {
console.log(results);
alert("Error!");
});
});
/**
* Third step, the REST endpoint itself
*/
class My_Endpoint {
public function registerRoutes() {
register_rest_route( \'my_plugin\', \'v1/my_endpoint\', [
\'methods\' => WP_REST_Server::READABLE,
\'callback\' => [ $this, \'get_something\' ],
\'args\' => [
\'some_value\' => [
\'required\' => true,
],
],
\'permission_callback\' => function ( WP_REST_Request $request ) {
return true;
},
] );
}
/**
* @return WP_REST_Response
*/
private function get_something( WP_REST_Request $request ) {
//if ( ! wp_verify_nonce( $request[\'nonce\'], \'action_nonce\' ) ) {
// return false;
//}
$some_value = $request[\'some_value\'];
if ( strlen( $some_value ) < 5 ) {
return new WP_REST_Response( \'Sorry, Some Value must be at least 5 characters long.\', 400 );
}
// Since we are passing the "X-WP-Nonce" header, this will work:
$user = wp_get_current_user();
if ( $user instanceof WP_User ) {
return new WP_REST_Response( \'Sorry, could not get the name.\', 400 );
} else {
return new WP_REST_Response( \'Your username name is: \' . $user->display_name, 200 );
}
}
}