admin管理员组文章数量:1130349
I have been using WordPress for the past two years and I always used plugins for additional functionality. But now I want to learn more about WordPress. I want to learn how to change things using html and how to use a json URL to call WordPress get_userdata from the database but I don't know how.
My question is:
Can I do that? If so, how can i use a json URL in order to get user information in wordpress? ( If needed I have SSL certificate)
Any answers will be helpful.
I already checked this but I don't understand how this happened
Using WordPress User Details outside Directory
I am creating an android app using android studio
I wanted to create login screen on my Android app so users would be able to login or sign up from android app
I have been using WordPress for the past two years and I always used plugins for additional functionality. But now I want to learn more about WordPress. I want to learn how to change things using html and how to use a json URL to call WordPress get_userdata from the database but I don't know how.
My question is:
Can I do that? If so, how can i use a json URL in order to get user information in wordpress? ( If needed I have SSL certificate)
Any answers will be helpful.
I already checked this but I don't understand how this happened
Using WordPress User Details outside Directory
I am creating an android app using android studio
I wanted to create login screen on my Android app so users would be able to login or sign up from android app
Share Improve this question edited Dec 23, 2018 at 0:46 fuxia♦ 107k39 gold badges255 silver badges461 bronze badges asked Jan 21, 2016 at 13:11 M ASED AHMEDM ASED AHMED 17 bronze badges 2- 1 Please share an example of the URL structure that would suit makemedroid requirements. – Sagive Commented Jan 21, 2016 at 13:51
- like twitter or facebook uses – M ASED AHMED Commented Jan 30, 2017 at 16:57
1 Answer
Reset to default 1It's possible to write your own endpoing using add_rewrite_rule().
In this case, we'll register the endpoint http://example/api/user_data/{id}.
Then check to make sure the ID is numeric. If it is, lookup the user by id and output the JSON response.
URL
http://example/api/user_data/1
JSON
{
"success": true,
"data": {
"id": 1,
"display_name": "your_username",
"user_registered": "2015-11-12 05:00:00",
"first": "First",
"last": "Last Name"
}
}
PHP
Put this in functions.php or in a plugin then refresh your permalinks.
if ( ! class_exists( 'JSONEndpoint_UserData' ) ):
/**
* The code that registers the endpoint and handles the result
*/
class JSONEndpoint_UserData {
const ENDPOINT_NAME = 'api/user_data'; // endpoint to capture
const ENDPOINT_QUERY_NAME = '__api_user_data'; // turns to param
// WordPress hooks
public function run() {
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
add_action( 'parse_request', array( $this, 'sniff_requests' ), 0 );
add_action( 'init', array( $this, 'add_endpoint' ), 0 );
}
// Add public query vars
public function add_query_vars( $vars ) {
$vars[] = static::ENDPOINT_QUERY_NAME;
$vars[] = 'id';
return $vars;
}
// Add API Endpoint
public function add_endpoint() {
add_rewrite_rule( '^' . static::ENDPOINT_NAME . '/([^/]+)/?$', 'index.php?' . static::ENDPOINT_QUERY_NAME . '=1&id=$matches[1]', 'top' );
// --->
flush_rewrite_rules( true ); //// <---------- REMOVE THIS WHEN DONE TESTING
// --->
}
// Sniff Requests
public function sniff_requests( $wp_query ) {
global $wp;
if ( isset( $wp->query_vars[ static::ENDPOINT_QUERY_NAME ] ) ) {
$this->handle_request(); // handle it
}
}
// Handle Requests
protected function handle_request() {
global $wp;
// we only deal with number$
$id = is_numeric( $wp->query_vars[ 'id' ] ) ? absint( $wp->query_vars[ 'id' ] ) : false;
if ( ! is_numeric( $id ) || ! $user = get_user_by( 'id', $id ) ) {
wp_send_json_error( array( 'message' => 'Invalid User ID' ) );
}
// ALLOWING ACCESS FROM ANYWHERE --- WE MIGHT WANT TO RESTRICT THE PLACES THAT CAN USE THIS
header( "Access-Control-Allow-Origin: *" );
// prep the response
$data = array(
'id' => $user->ID,
'display_name' => $user->data->display_name,
'user_registered' => $user->data->user_registered,
'first' => $user->first_name,
'last' => $user->last_name,
);
// write the response
wp_send_json_success( $data );
die(); // just in case
}
}
$ep = new JSONEndpoint_UserData();
$ep->run();
endif; // JSONEndpoint_UserData
I have been using WordPress for the past two years and I always used plugins for additional functionality. But now I want to learn more about WordPress. I want to learn how to change things using html and how to use a json URL to call WordPress get_userdata from the database but I don't know how.
My question is:
Can I do that? If so, how can i use a json URL in order to get user information in wordpress? ( If needed I have SSL certificate)
Any answers will be helpful.
I already checked this but I don't understand how this happened
Using WordPress User Details outside Directory
I am creating an android app using android studio
I wanted to create login screen on my Android app so users would be able to login or sign up from android app
I have been using WordPress for the past two years and I always used plugins for additional functionality. But now I want to learn more about WordPress. I want to learn how to change things using html and how to use a json URL to call WordPress get_userdata from the database but I don't know how.
My question is:
Can I do that? If so, how can i use a json URL in order to get user information in wordpress? ( If needed I have SSL certificate)
Any answers will be helpful.
I already checked this but I don't understand how this happened
Using WordPress User Details outside Directory
I am creating an android app using android studio
I wanted to create login screen on my Android app so users would be able to login or sign up from android app
Share Improve this question edited Dec 23, 2018 at 0:46 fuxia♦ 107k39 gold badges255 silver badges461 bronze badges asked Jan 21, 2016 at 13:11 M ASED AHMEDM ASED AHMED 17 bronze badges 2- 1 Please share an example of the URL structure that would suit makemedroid requirements. – Sagive Commented Jan 21, 2016 at 13:51
- like twitter or facebook uses – M ASED AHMED Commented Jan 30, 2017 at 16:57
1 Answer
Reset to default 1It's possible to write your own endpoing using add_rewrite_rule().
In this case, we'll register the endpoint http://example/api/user_data/{id}.
Then check to make sure the ID is numeric. If it is, lookup the user by id and output the JSON response.
URL
http://example/api/user_data/1
JSON
{
"success": true,
"data": {
"id": 1,
"display_name": "your_username",
"user_registered": "2015-11-12 05:00:00",
"first": "First",
"last": "Last Name"
}
}
PHP
Put this in functions.php or in a plugin then refresh your permalinks.
if ( ! class_exists( 'JSONEndpoint_UserData' ) ):
/**
* The code that registers the endpoint and handles the result
*/
class JSONEndpoint_UserData {
const ENDPOINT_NAME = 'api/user_data'; // endpoint to capture
const ENDPOINT_QUERY_NAME = '__api_user_data'; // turns to param
// WordPress hooks
public function run() {
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
add_action( 'parse_request', array( $this, 'sniff_requests' ), 0 );
add_action( 'init', array( $this, 'add_endpoint' ), 0 );
}
// Add public query vars
public function add_query_vars( $vars ) {
$vars[] = static::ENDPOINT_QUERY_NAME;
$vars[] = 'id';
return $vars;
}
// Add API Endpoint
public function add_endpoint() {
add_rewrite_rule( '^' . static::ENDPOINT_NAME . '/([^/]+)/?$', 'index.php?' . static::ENDPOINT_QUERY_NAME . '=1&id=$matches[1]', 'top' );
// --->
flush_rewrite_rules( true ); //// <---------- REMOVE THIS WHEN DONE TESTING
// --->
}
// Sniff Requests
public function sniff_requests( $wp_query ) {
global $wp;
if ( isset( $wp->query_vars[ static::ENDPOINT_QUERY_NAME ] ) ) {
$this->handle_request(); // handle it
}
}
// Handle Requests
protected function handle_request() {
global $wp;
// we only deal with number$
$id = is_numeric( $wp->query_vars[ 'id' ] ) ? absint( $wp->query_vars[ 'id' ] ) : false;
if ( ! is_numeric( $id ) || ! $user = get_user_by( 'id', $id ) ) {
wp_send_json_error( array( 'message' => 'Invalid User ID' ) );
}
// ALLOWING ACCESS FROM ANYWHERE --- WE MIGHT WANT TO RESTRICT THE PLACES THAT CAN USE THIS
header( "Access-Control-Allow-Origin: *" );
// prep the response
$data = array(
'id' => $user->ID,
'display_name' => $user->data->display_name,
'user_registered' => $user->data->user_registered,
'first' => $user->first_name,
'last' => $user->last_name,
);
// write the response
wp_send_json_success( $data );
die(); // just in case
}
}
$ep = new JSONEndpoint_UserData();
$ep->run();
endif; // JSONEndpoint_UserData
本文标签: pluginsAdd wordpress user database on android app
版权声明:本文标题:plugins - Add wordpress user database on android app 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749074932a2312154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论