First time creating custom endpoints. Ill be fetching posts from this site from another wordpres install.
Here is my plugin code.
/**
* Add endpoint URLs
*/
public function __construct(){
add_action('rest_api_init', function(){
// Get submissions from the database
register_rest_route('knpv-get','get-submissions', array(
'methods'=>'GET',
'callback' => 'get_submissions_by_email'
)
);
});
}
/**
* Get submisions by email endpoint callback
*/
public function get_submissions_by_email($data){
$posts = get_posts();
return $posts;
}
But all I get when i view the url (domain/wp-json/knpv-get/) in browser is:
{"namespace":"knpv-get","routes":{"\/knpv-get":{"namespace":"knpv-get","methods":["GET"],"endpoints":[{"methods":["GET"],"args":{"namespace":{"required":false,"default":"knpv-get"},"context":{"required":false,"default":"view"}}}],"_links":{"self":"http:\/\/knoppysdev\/vendor-portal\/wp-json\/knpv-get"}},"\/knpv-get\/get-submissions":{"namespace":"knpv-get","methods":["GET"],"endpoints":[{"methods":["GET"],"args":[]}],"_links":{"self":"http:\/\/knoppysdev\/vendor-portal\/wp-json\/knpv-get\/get-submissions"}}},"_links":{"up":[{"href":"http:\/\/knoppysdev\/vendor-portal\/wp-json\/"}]}}
What am I missing, shouldn't it return some posts. Its a fresh install so there's only Hello World to return.
First time creating custom endpoints. Ill be fetching posts from this site from another wordpres install.
Here is my plugin code.
/**
* Add endpoint URLs
*/
public function __construct(){
add_action('rest_api_init', function(){
// Get submissions from the database
register_rest_route('knpv-get','get-submissions', array(
'methods'=>'GET',
'callback' => 'get_submissions_by_email'
)
);
});
}
/**
* Get submisions by email endpoint callback
*/
public function get_submissions_by_email($data){
$posts = get_posts();
return $posts;
}
But all I get when i view the url (domain/wp-json/knpv-get/) in browser is:
{"namespace":"knpv-get","routes":{"\/knpv-get":{"namespace":"knpv-get","methods":["GET"],"endpoints":[{"methods":["GET"],"args":{"namespace":{"required":false,"default":"knpv-get"},"context":{"required":false,"default":"view"}}}],"_links":{"self":"http:\/\/knoppysdev\/vendor-portal\/wp-json\/knpv-get"}},"\/knpv-get\/get-submissions":{"namespace":"knpv-get","methods":["GET"],"endpoints":[{"methods":["GET"],"args":[]}],"_links":{"self":"http:\/\/knoppysdev\/vendor-portal\/wp-json\/knpv-get\/get-submissions"}}},"_links":{"up":[{"href":"http:\/\/knoppysdev\/vendor-portal\/wp-json\/"}]}}
What am I missing, shouldn't it return some posts. Its a fresh install so there's only Hello World to return.
Share Improve this question edited Jun 25, 2019 at 13:31 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Jun 25, 2019 at 11:48 Alex KnoppAlex Knopp 1135 bronze badges1 Answer
Reset to default 1You're using the wrong route. The first two arguments of register_rest_route()
ar the namespace, and the route. The URL to a REST API endpoint is:
(your domain) + '/wp-json/' + (namespace) + '/' + (route)
So the full URL to your endpoint is:
http://domain/wp-json/knpv-get/get-submissions
But you're attempting to view:
http://domain/wp-json/knpv-get
Which won't return the response from your callback function. However if you look closely at the result, you'll see that it's giving you a list of routes under the knpv-get
namespace:
{
"namespace": "knpv-get",
"routes": {
"\/knpv-get": {
"namespace": "knpv-get",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"namespace": {
"required": false,
"default": "knpv-get"
},
"context": {
"required": false,
"default": "view"
}
}
}
],
"_links": {
"self": "http:\/\/knoppysdev\/vendor-portal\/wp-json\/knpv-get"
}
},
"\/knpv-get\/get-submissions": {
"namespace": "knpv-get",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": [
]
}
],
"_links": {
"self": "http:\/\/knoppysdev\/vendor-portal\/wp-json\/knpv-get\/get-submissions"
}
}
},
"_links": {
"up": [
{
"href": "http:\/\/knoppysdev\/vendor-portal\/wp-json\/"
}
]
}
}