I am using JSON API for get the post list from my App.
I need to create user(Register new user) from App. May I know how to add new function to Wordpress JSON-API?
Regards, Alex
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI am using JSON API for get the post list from my App.
I need to create user(Register new user) from App. May I know how to add new function to Wordpress JSON-API?
Regards, Alex
I've never worked with that plugin and a new JSON Rest API will maybe be included in core with one of the future versions. So far it's as well available as plugin.
To make it short: There currently seems to only be three kinds of Controllers in that plugin
But there's a section on the main plugin page that explains how to extend the plugin and how to develop JSON API Controllers. So It's actually just a slightly modified copypaste from what you could have read in the very excellent and detailed plugin page !
class JSON_API_Hello_Controller
{
public function hello_world()
{
return array(
"message" => "Hello, world"
);
}
}
Your controller is now available as
hello
, and exposes onehello_world
method.Next, activate your controller from the WordPress admin interface, available from the menu under Settings > JSON API. You can either click on the link to your
hello_world
method from the admin interface or enter it manually. It should have the form:http://www.example.org/api/hello/hello_world/?dev=1
orhttp://www.example.org/?json=hello.hello_world&dev=1
(note the use of thedev
argument to enable human-readable output). You should get the following output:
{
"status": "ok",
"message": "Hello, world"
}
To customize the behavior of your controller, you will want to make use of the global
$json_api->query
object. Add the following method to your controller:
public function hello_person()
{
global $json_api;
$name = $json_api->query->name;
return array(
"message" => "Hello, $name."
);
}
Now append the name query var to the method call:
http://www.example.org/api/hello/hello_world/?dev=1&name=Alice
...or...
http://www.example.org/?json=hello.hello_world&dev=1&name=Alice.
The result will be:
{
"status": "ok",
"message": "Hello, Alice"
}
To make your controller visible from an external plugin or theme directory you will need to use two filters:
json_api_controllers
andjson_api_[controller]_controller_path
. Just create a custom plugin that holds your controller and add the following code:
<?php
/** Plugin Name: (#109019) Custom Hello JSON API Controller */
function add_hello_controller( $controllers )
{
$controllers[] = 'hello';
return $controllers;
}
add_filter( 'json_api_controllers', 'add_hello_controller' );
function set_hello_controller_path()
{
return plugin_dir_path( __FILE__ )."helloController.php";
}
add_filter( 'json_api_hello_controller_path', 'set_hello_controller_path' );
// >> Controller goes here <<
// A more organized file structure might work as well, but it's ok
// to stuff everything in a single file if used as mu-plugin.
You can use any sort of WP public API functions in your Controller. Still JSON API has a middle ware layer as well. Example:
global $json_api;
$posts = $json_api->introspector->get_posts( array(
'meta_key' => $json_api->query->key,
'meta_value' => $json_api->query->value
) );
Try the JSON API User plugin. It advertises that it can register a new user for you.
Use WP API v2, it has Users Controller implemented. If u are using V1, there is plugin someone mentioned above.