10

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?

  • What did you end up going with? I'm trying to create custom routes/endpoints for the wp rest api and 1) the documentation is terrible 2) there are zero examples around the web. Let me know what route you took please! – Jacksonkr Apr 27 '15 at 16:34
  • 1
    I decided to go with building my own API outside of Wordpress, but that has access to the same database. I never actually got around to it though, since this was just a side project and I've been too busy with other things :P. If I were you I would have a look into the symfony2 or laravel frameworks. They are really quick and easy to get something up and running. – Yep_It's_Me Apr 27 '15 at 22:27
15

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.

  • 3
    Really good answer +1. – gmazzap Sep 29 '14 at 9:23
  • 2
    Double that. +1 – kaiser Sep 29 '14 at 9:47
  • Thanks for the really great answer! So from that, the two options I have are: 1. write my own plugin; 2. write a separate service. Since all I need the endpoint to do is take a few GET parameters and return a JSON body, I think I should write my own service because "Loading the entire core just to save a few lines of code using wpdb is no worth it". – Yep_It's_Me Sep 30 '14 at 0:14
0

API Endpoints plugin should cover all your needs, especially if you are familiar with MySQL. You would do the following steps:

  1. Create a new Twig Template, enter your MysQL query to fetch data from your WordPress site and use Twig syntax to output the data in any format. For example, JSON.

  2. Create a new API endpoint, give it a name and configure it to use the Twig Template you created in step 1.

Once done, repeat steps 1 and 2 for every endpoint you would like to create. API Endpoints uses standard WordPress authentication system, so it would be best to create a separate user in your WordPress and use its username/password to access your api endpoints.

  • API Endpoints looks interesting for a project of mine but it requires Twig Anything which is almost impossible to get as it requires login in your blog which doesn't allow public subscriptions. – mettjus Sep 13 '15 at 9:58
  • I now realised it is not free... – mettjus Sep 13 '15 at 10:00
0

The new version of WP REST API is in Beta, but has pretty good documentation around how to register custom REST endpoints. There is also a very small blurb about how it could be done in version 1, which I am going to try out since the beta is not guaranteed to be or stay stable. I assume you could then use wpdb from within the methods you register. I am actually just using ACF stuff and prepossessing it into objects in my php to create a nice, searchable/sortable multi-property index page with angularJS.

Comparison Between WWP REST API 1 and 2 from their docs

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.