Building a custom REST API

I have a WordPress.org site which takes user input, searches a custom database and displays the results of the query. I have this working using the wpdb class. Now I’m trying to get this working for an android app.

From what I understand, building a RESTful server is the current ‘best practice’ for this kind of thing. I have looked into some plugins that could help me do this, and I have come across:

  1. json-rest-api – It seems to me that this only exposes the core WordPress functionality (ie. posts, pages, users, etc.) and I can’t see how to get this to execute custom queries on non-default tables.

  2. This blog post says that wp mvc does exactly what I want, except it hasn’t been updated in over 2 years, so that kinda rules that out.

  3. There is also Jetpack but from what I’ve seen, the json-rest-api is the updated version of this and it seems to only give core functionality as well.

Is there a way to make any of these plugins work for custom endpoints? Or are there other plugins I haven’t found yet?

If I don’t use a plugin, I figure I would have to roll my own. I can do this either from scratch (using wpdb, msqli or something), or with the help of an API such as Restler.

If I do roll my own should I create a template in my theme, do the REST stuff in there and assign that theme to a page within the dashboard? Or would it be better to have another server running separate to WordPress for this?

As far as I can see, these are my only options. I would like to know what are the recommended best practices for this kind of thing. I find it hard to believe that no one else has done this. Are there any options I have overlooked?

3 s
3

TL;DR

Yes, WordPress can certainly act as a backend for a mobile app. Yes, a page can act as a rest endpoint / interface. No, a theme template is not the right territory for the logic. Write your own plugin.


Pointers

I find it hard to believe that no one else has done this.

I, for one, have. More than once. And I’m near certain I’m not alone. “No one has extensively blogged about it” is probably the more correct notion.

If I don’t use a plugin, I figure I would have to roll my own. I can do this either from scratch (using wpdb, msqli or something), or with the help of an API such as Restler.

I don’t know “Restler” and that is out of scope on this stack anyway.
As far as “using wpdb, msqli or something” is concerned: You would certainly use wpdb to save (received) data to the database, but it is not pertinent to the endpoint logic.

A theme template is not what you should look into. Themes are meant for visual presentation. A REST endpoint does not need a visual appearance at all.

Insert the endpoint into the page you want to use for it via a shortcode.

Have the shortcode handler / callback listen to either HTTP POST or GET and invoke data saving or other secondary methods accordingly.


Does it make sense to use WordPress as an endpoint / backend?

It depends.
If all you need is an endpoint to save data: No. Loading the entire core just to save a few lines of code by using wpdb is not worth it.
If you need a backend that can be logged into via a web browser, that can display tabular data, offer ways to alter said data, maybe even with multiple access levels / user roles and rights, then yes, it does make sense.

Leave a Comment