4

I have been working on a Wordpress plugin and I come from a background where I usually write my own REST endpoints (Rails etc). My question is, how do I from a WP plugin create rest url endpoints?

E.g.:

/myplugin/save-tutorial (POST takes JSON and returns JSON) /myplugin/get-tutorial?id= (GET returns JSON)

How do I create such REST endpoints? I have looked at admin-ajax.php and that seems about right, but still pretty messy. It seems like a simple problem though. I want to process the responses in my-plugin.php.

Thanks in advance!

3
  • 1
    Not yet implemented by me, but dig this plugin wordpress.org/plugins/json-rest-api might be its helpful for you, to create endpoints for the API. Commented Mar 27, 2014 at 11:07
  • Yes but if I understand the description of the plugin correctly, isn't that plugin just creating JSON endpoints for the regular pages? So you can use a custom frontend for WP? Commented Mar 27, 2014 at 11:15
  • I'd research here Commented Mar 27, 2014 at 12:07

2 Answers 2

1

You should use add_rewrite_endpoint() for this purposes. This function creates extra rewrite rules for each of the matching places specified by the provided bitmask. For example: add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES );

will add a new rewrite rule ending with json(/(.*))?/?$ for every permastruct that describes a permalink (post) or page. This is rewritten to json=$match where $match is the part of the URL matched by the endpoint regex (e.g. foo in <permalink>/json/foo/).

You can also read this guide https://make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/

Sign up to request clarification or add additional context in comments.

1 Comment

I solved this with instead of using actual endpoints for my Backbone.js app, I added some actions in Wordpress, and then used their JSON-RPC api for it. Here is a link: codex.wordpress.org/Function_Reference/add_action
0

You can use

register_rest_route ( string $namespace, string $route, array $args = array(), bool $override = false )

function to register a rest api in wordpress.

you can look in to this sample snippet http://wiki.workassis.com/wordpress-create-rest-api/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.