A5下载 - 努力做内容最丰富最安全的下载站!

A5站长下载站

当前位置:A5下载 > 源码技巧 > wordpress > WordPress教程:自定义WP REST API (WP API)授权

WordPress教程:自定义WP REST API (WP API)授权

时间:2015-07-25 11:44作者:fang人气:606

* 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 站点进行交互。

标签WordPress,教程,自定义,REST,API,授权,@

相关下载

查看所有评论+

网友评论

网友
您的评论需要经过审核才能显示

热门阅览

最新排行

公众号