I want to use Wordpress REST API to Login and get user data on android app.
I am using wp_users table for user and a custom table for user info .
I tried writing my own REST API but i for that i have to make $wpdb accessible outside Wordpress installation.
Please provide yours solution having API-KEY , or something similar , feature too.
Thanks
I want to use Wordpress REST API to Login and get user data on android app.
I am using wp_users table for user and a custom table for user info .
I tried writing my own REST API but i for that i have to make $wpdb accessible outside Wordpress installation.
Please provide yours solution having API-KEY , or something similar , feature too.
Thanks
Share Improve this question asked Apr 15, 2016 at 7:56 Divyanshu JimmyDivyanshu Jimmy 4493 gold badges9 silver badges20 bronze badges 4- Have you found the solution ? – Mithun Sarker Commented May 18, 2016 at 6:12
- @shuvro , yes i have the solution , please check my answer. all the best with yours product – Divyanshu Jimmy Commented May 19, 2016 at 7:35
- Have you read this post on make? – kaiser Commented Oct 23, 2016 at 13:10
- man your solution was great but wordpress passwords are hashed :/ how did you log in ? – Daniyal Javaid Commented Jul 26, 2018 at 19:09
2 Answers
Reset to default 7I found the simplest solution using the WP-REST API plugin,first set this in yours environment :
1.) In your themes functions.php
register API endpoint hooks:
add_action( 'rest_api_init', 'register_api_hooks' );
// API custom endpoints for WP-REST API
function register_api_hooks() {
register_rest_route(
'custom-plugin', '/login/',
array(
'methods' => 'POST',
'callback' => 'login',
)
);
function login() {
$output = array();
// Your logic goes here.
return $output;
}
2.) By default, if you have pretty permalinks enabled, the WordPress REST API “lives” at /wp-json/. Then the API endpoint is accessible at youdomain/wp-json/custom-plugin/login
with a POST
request.
Notice that custom-plugin/login is actually defined in register_rest_route in PHP function register_api_hooks()
For API key I am using Wordpress Nonces - pretty straightforward as in my discussion here . I hope these answers are useful for all full stack developers who are new to Wordpress REST API
If you just want to user login and get user details you can use and excellent plugin called "JSON API AUTH"
https://wordpress/plugins/json-api-auth/
There are following methods available: validate_auth_cookie
, generate_auth_cookie
, clear_auth_cookie
, get_currentuserinfo
nonce can be created by calling http://localhost/api/get_nonce/?controller=auth&method=generate_auth_cookie
You can then use ‘nonce’ value to generate cookie. http://localhost/api/auth/generate_auth_cookie/?nonce=f4320f4a67&username=Catherine&password=password-here
Use cookie like this with your other controller calls: http://localhost/api/contoller-name/method-name/?cookie=Catherine|1392018917|3ad7b9f1c5c2cccb569c8a82119ca4fd
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745173981a4615098.html
评论列表(0条)