Building a custom REST API

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