1

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

1
  • Why you will change, enhance the WP JSON API. You can use the default functions to create a user and use your json content data to use the values on the functions.
    – bueltge
    Aug 5, 2013 at 12:22

3 Answers 3

1

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

  1. Core
  2. Posts
  3. Respond

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 one hello_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 or http://www.example.org/?json=hello.hello_world&dev=1 (note the use of the dev argument to enable human-readable output). You should get the following output:

{
    "status": "ok",
    "message": "Hello, world"
}

Using query vars

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 and json_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

) );

1
  • 2
    A small hint about the version of the WP API should be helpful. The v2 is currently in Beta status and the v1 is legacy. I think is helpful for readers in the future. Your hooks from the v2, but is this solid in the WP universe ;)
    – bueltge
    Dec 2, 2015 at 15:28
0

Try the JSON API User plugin. It advertises that it can register a new user for you.

0

Use WP API v2, it has Users Controller implemented. If u are using V1, there is plugin someone mentioned above.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.