By Sean

2019-02-10 20:50:25 8 Comments

I've defined a REST endpoint /my-plugin/v1/post-read to show whether a post has been read or not, per-user. In PHP, my plugin keeps track of this depending on page views while logged in.

I'm now trying to add a front-end AJAX control to let users manually set a post as "unread" or "read". I've read that the WordPress REST API comes with a JavaScript library for interacting with it, and this is bundled with WordPress too. According to the documentation, this also handles authentication and noncing transparently, and one must simply extend a base class:

For developers using the built-in Javascript API, this is handled automatically for you. This is the recommended way to use the API for plugins and themes. Custom data models can extend wp.api.models.Base to ensure this is sent correctly for any custom requests.

I'd like to use this so I don't have to deal with nonces. I've found an example how to use the library with the built-in post model, but I want to extend this for my custom endpoint.

I want to know whether it's appropriate to use and extend this library for sending POST requests, and if so, how I extend wp.api.models.Base to support this simple endpoint. All the examples I've found are for built-in objects like post, taxonomy, etc.


Related Questions

Sponsored Content

1 Answered Questions

Using the REST API (v2) javascript client on a private namespaced route

  • 2016-07-19 17:56:33
  • Dalton
  • 399 View
  • 5 Score
  • 1 Answer
  • Tags:   rest-api backbone

1 Answered Questions

Wp Rest API request posts from a custom taxonomy

1 Answered Questions

[SOLVED] WP REST API Post Status Using JavaScript

1 Answered Questions

1 Answered Questions

[SOLVED] Match REST API post output from custom endpoint

  • 2017-01-23 22:34:47
  • joemaller
  • 1076 View
  • 2 Score
  • 1 Answer
  • Tags:   rest-api

2 Answered Questions

[SOLVED] Can I use REST-API on plain permalink format?

  • 2017-07-12 14:24:46
  • Jack Johansson
  • 2702 View
  • 8 Score
  • 2 Answer
  • Tags:   permalinks rest-api

0 Answered Questions

custom REST endpoint not passing body of POST request to callback

  • 2017-09-15 11:10:30
  • Jesterscup
  • 374 View
  • 0 Score
  • 0 Answer
  • Tags:   plugins rest-api

1 Answered Questions

[SOLVED] Custom rest api endpoint response json problem

1 Answered Questions

[SOLVED] WP Rest endpoint with custom post type and ACF Fields

4 Answered Questions

[SOLVED] REST API: Backbone and custom endpoint

  • 2017-05-14 23:38:49
  • Lucio Crusca
  • 1047 View
  • 5 Score
  • 4 Answer
  • Tags:   javascript rest-api

Sponsored Content