-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordpress_plugin_front_end
More file actions
41 lines (36 loc) · 1.17 KB
/
Copy pathwordpress_plugin_front_end
File metadata and controls
41 lines (36 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
/*
Plugin Name: Custom REST Endpoints
Description: Adds custom REST API endpoints to retrieve user role by ID.
Version: 1.0
Author: Bohuslav Sedláček
*/
// Register custom REST API endpoint
add_action('rest_api_init', function () {
register_rest_route('custom-endpoints/v1', '/user-role/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'get_user_role_by_id',
'permission_callback' => 'api_key_check' // Updated permission callback
));
});
// Callback function to retrieve user role by ID
function get_user_role_by_id($request) {
$user_id = $request['id'];
$user = get_userdata($user_id);
if ($user) {
return rest_ensure_response($user->roles);
} else {
return new WP_Error('user_not_found', 'User not found', array('status' => 404));
}
}
// Updated function to check for a valid API key
function api_key_check($request) {
$api_key = $request->get_header('x-api-key');
if (defined('MY_API_KEY') && $api_key === MY_API_KEY) {
return true; // Return true if API key matches
} else {
// Return false if not matched, don't directly return WP_Error here
return false;
}
}
?>