1

I am an Android developer and new to WordPress.

Requirement : is to write rest APIs to Login and get User details after login.

All the plugins and wp-rest apis only expose core functionalities and all the questions on this stack are of much advanced level which i am unable to understand.

I have an idea that i can get json response editing functions.php, but i am not sure about this.

Please give a short example on Where and How i should write my rest apis using Wordpress .

Thanks.

closed as too broad by TheDeadMedic, fuxia Apr 27 '16 at 1:39

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    The WP REST API plugin is halfway through the process of being integrated into core, I would advise you to learn how that works rather than try to write something new. – Milo Apr 14 '16 at 5:02
  • @Milo can you sight some example on how to use wp rest api plugin for custom table ? – Divyanshu Jimmy Apr 14 '16 at 5:03
  • 1
    The documentation has examples for adding your own custom routes. Within your custom handlers, you can query your own tables using the wpdb class. – Milo Apr 14 '16 at 5:07
  • @Milo Thanks , i will start studying and developing using yours guide ,if i struck somewhere i wil contact you here . :) – Divyanshu Jimmy Apr 14 '16 at 5:14
  • You can edit your question at any time to add whatever code you're currently working with, even if it doesn't work. – Milo Apr 14 '16 at 5:19
2

Hopefully someone will extend on my answer....

The critical part of the rest API that is already in core is the registration and routing of endpoints which should look like (taken from http://v2.wp-api.org/extending/adding/)

`/**
 * Grab latest post title by an author!
 *
 * @param array $data Options for the function.
 * @return string|null Post title for the latest,
     
 * or null if none.
 */
function my_awesome_func( $data ) {
    $posts = get_posts( array(
        'author' => $data['id'],
    ) );`

    if ( empty( $posts ) ) {
        return null;
    }

    return $posts[0]->post_title;
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
        'methods' => 'GET',
        'callback' => 'my_awesome_func',
    ) );
} );

Based on this pattern you can just handle your API in whatever way you prefer.

I advice against using the core rest API, unless you need information in vanilla wordpress format. The wordpress rest API does both too much and too little for what most non trivial APIs will do, and it is just better to isolate your logic.

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