rest api - Custom Endpoints not working

First time creating custom endpoints. Ill be fetching posts from this site from another wordpres install. Here is my plu

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

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/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\/"
      }
    ]
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745371350a4624809.html

相关推荐

  • rest api - Custom Endpoints not working

    First time creating custom endpoints. Ill be fetching posts from this site from another wordpres install. Here is my plu

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信