Reputation: 361
I have a WordPress website and I want to add a custom endpoint that sends the metadata of the users.
Important: The already API works only by calling it like this "domain-name.com/?rest_route=/wp/v2/users/". I know that for the API to work properly the standard solution for a custom endpoint is to change the permalink settings from plain to post.
Is there any solution without changing that setting?
The standard pre-built endpoints work fine when I call them like in the above example.
For example this code only works for the wordpress websites with the permalink settings as post. Calling the api like this (domain.com/wp-json/wp/v2/custom-ep. If I place the same code in the other website with permalink setting and call it as mentioned above ("domain-name.com/?rest_route=/wp/v2/users/"), I will get error 404.
code:
function create_custom_endpoint()
{
register_rest_route(
'wp/v2', // Namespace
'/custom-ep', // Route
array(
'methods' => 'GET', // HTTP method
'callback' => 'get_response', // Callback function
)
);
}
function get_response()
{
// This is your custom response data
return 'This is your data!';
}
// Hook the custom endpoint registration to rest_api_init
add_action('rest_api_init', 'create_custom_endpoint');
Upvotes: 1
Views: 73
Reputation: 361
I was adding the script/snippet in the wrong functions.php file (wrong theme). I added the code in the correct functions.php and it worked.
Upvotes: 0