0

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.com\/vendor-portal\/wp-json\/knpv-get"}},"\/knpv-get\/get-submissions":{"namespace":"knpv-get","methods":["GET"],"endpoints":[{"methods":["GET"],"args":[]}],"_links":{"self":"http:\/\/knoppysdev.com\/vendor-portal\/wp-json\/knpv-get\/get-submissions"}}},"_links":{"up":[{"href":"http:\/\/knoppysdev.com\/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.

0

You'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.com/wp-json/knpv-get/get-submissions

But you're attempting to view:

http://domain.com/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.com\/vendor-portal\/wp-json\/knpv-get"
      }
    },
    "\/knpv-get\/get-submissions": {
      "namespace": "knpv-get",
      "methods": [
        "GET"
      ],
      "endpoints": [
        {
          "methods": [
            "GET"
          ],
          "args": [

          ]
        }
      ],
      "_links": {
        "self": "http:\/\/knoppysdev.com\/vendor-portal\/wp-json\/knpv-get\/get-submissions"
      }
    }
  },
  "_links": {
    "up": [
      {
        "href": "http:\/\/knoppysdev.com\/vendor-portal\/wp-json\/"
      }
    ]
  }
}
  • thanks for the above, Ive used that URL and am returned with {"code":"rest_invalid_handler","message":"The handler for the route is invalid","data":{"status":500}} – Alex Knopp Jun 25 at 13:18
  • Ive used this as the answer because it does solve the issue in the question. Also worth noting that the 'callback' method had to be 'callback' => array($this, 'get_submissions_by_email') to work. – Alex Knopp Jun 25 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.