分类分类
更新时间:2015-07-25 11:44作者:fang
* A {@see WP_Error} instance can be returned if an error occurs, and
* this should match the format used by API methods internally (that is,
* the `status` data should be used). A callback can return `true` to
* indicate that the authentication method was used, and it succeeded.
*
* @param WP_Error|null|boolean WP_Error if authentication error, null if authentication method wasn't used, true if authentication succeeded
*/
return apply_filters( 'json_authentication_errors', null );
}
基于上面的这个函数以及其被调用位置,我们可以加进去一个hook,以确认认证是否成功:
/**
* WP JSON API 认证检查
* @param null
* @return boolean 是否认证成功
* @author suifengtec coolwp.com
*/
function coolwp_rest_api_auth_check( $result ){
if(
!isset($_GET['id'])
// ||!isset($_GET['app_key'])
||!isset($_GET['app_token'])
||empty($_GET['id'])
// ||empty($_GET['app_key'])
||empty($_GET['app_token'])
){
return false;
}
//获取从应用GET过来的用户id、app_key和app_token,当然了,你也可以只用一个去app_key和app_token中的任何一个去检查
$user_id = (int)$_GET['id'];
// $app_key = sanitize_text_field($_GET['app_key']);
$app_token = sanitize_text_field($_GET['app_token']);
//查询app_key和app_token,当然了,你也可以自定义一种算法,
//$wp_key = get_user_meta( $user_id, 'app_key', true);
$wp_token = get_user_meta( $user_id, 'app_token', true);
//将从应用客户端获取到的值与数据库存储的值进行对比
if(
( $wp_token == $app_token )
// &&( $wp_key == $app_key )
){
return true;
}
return false;
}
add_filter('json_authentication_errors', 'coolwp_rest_api_auth_check');
结论
加入 rest api 的 WordPress 甚至可以让你做一个在线支付网站,有了这组 api ,基于 WordPress 的原生安卓应用和IOS应用可以更好的与 WordPress 站点进行交互。
相关
纸片马里奥(Paperboat)角色扮演107.85 MBv1.0.32026-11-29
下载cyime输入法实用工具74.58 MBv1.15.1072026-11-29
下载武道仙宗官方版角色扮演456.19 Mv1.0.02026-11-29
下载mmrl模块app实用工具7.15 Mv342962026-11-29
下载零希沃特手机版角色扮演511.54 Mv1.4.22026-11-29
下载Bannerlator手游辅助574.84 MBv3.2.12026-11-29
下载巅峰激战(魔幻格斗游戏)动作射击797.1 MBv1.0.92026-11-29
下载我的火车防御策略游戏181.87 Mv1.1.52026-11-29
下载三国棋逢对手角色扮演1.7Gv1.2.5.0002.1125.11432026-11-29
下载图纸没问题手机版休闲益智78.41 Mv1.0.182026-11-29
下载蛊界永生角色扮演407.01 Mv0.4.162026-11-28
下载百炼仙途角色扮演528.9 Mv10002026-11-28
下载










