0

In the code example below I'm using the the post title (name) as the field to store the email address while the password is in a custom field. The following query will pull the correct user but it doesn't care if the password matches or not. I've scoured for hours and this is the best I have yet, what's missing?

$posts = $json_api->introspector->get_posts(array(
    'post_type'     => 'custom_user',
    'name'          => '[email protected]',
    'meta_query'    => array(
        'key'               => 'password',
        'value'             => 'pass123'
    )
));

Also, I'm using the wp json plugin located here

1
  • 1
    You probably should use WP REST API (WP API), which will sooner or later be part of the WordPress core. The one you're using hasn't been updated in along time and doesn't support recent WordPress versions. Commented Apr 23, 2015 at 17:45

1 Answer 1

1

Per the comment from @ialocin I switched from WP JSON API to WP REST API. There is much less documentation with the rest api but it's customizable using native wordpress functions. Also, it has a nifty github plugin for allowing use with ACF custom fiends.

Anyway, the documentatino for customization the wp rest api is terrible so I'm going to show you what I did as of wp 4.2.0 + wp rest api 1.2.1

First thing is to add an action to the plugin so that it will pick up your new rest actions. Do that in wp-content/plugins/[rest-api-folder]/plugins.php. I personally added all this at the bottom of the file.

require_once( dirname( __FILE__ ) . '/lib/class-myplugin-api-mytype.php' );

function myplugin_api_init() {
    global $myplugin_api_mytype;

    $myplugin_api_mytype = new MyPlugin_API_MyType();
    add_filter( 'json_endpoints', array( $myplugin_api_mytype, 'register_routes' ) );
}
add_action( 'wp_json_server_before_serve', 'myplugin_api_init' );

Now it's time to add your new class which is where the meat is. Create the file wp-content/plugins/[rest-api-folder]/lib/class-myplugin-api-mytype.php and put this into it:

<?php

class MyPlugin_API_MyType {
    public function register_routes( $routes ) {
        $routes['/test'] = array(
            array( array( $this, 'test'), WP_JSON_Server::READABLE ),
        );
        // $routes['/myplugin/mytypeitems'] = array(
            // array( array( $this, 'get_posts'), WP_JSON_Server::READABLE ),
            // array( array( $this, 'new_post'), WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON ),
        // );
        // $routes['/myplugin/mytypeitems/(?P<id>\d+)'] = array(
        //  array( array( $this, 'get_post'), WP_JSON_Server::READABLE ),
        //  array( array( $this, 'edit_post'), WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
        //  array( array( $this, 'delete_post'), WP_JSON_Server::DELETABLE ),
        // );

        // Add more custom routes here

        return $routes;
    }

    public function test( $filter = array(), $context = 'view', $type = 'post', $page = 1 ) {
        return array("status" => "ok", "message" => "it worked!");
    }
}

?>

Now go to your worpress page with "/wp-json/test" at the end of the url (no querystring) and you should see

{"status":"ok","message":"it worked!"}

Viola! Also you can look at /libs/class-wp-json-posts.php and copy mechanisms in there as needed. I hope this saves someone all the time it took me to figure this out!

Your Answer

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

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