国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

WordPress教程:自定義WP REST API (WP API)授權

2024-04-25 20:39:58
字體:
來源:轉載
供稿:網友

* 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 站點進行交互。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 萨迦县| 乌兰察布市| 闽清县| 龙山县| 左云县| 池州市| 柘荣县| 庆城县| 冀州市| 齐河县| 体育| 乌审旗| 皋兰县| 陇南市| 德安县| 分宜县| 大名县| 门源| 舟曲县| 佳木斯市| 蒲城县| 商丘市| 如皋市| 渭南市| 仁布县| 嘉祥县| 通江县| 三江| 辽阳县| 博野县| 霸州市| 乌拉特前旗| 赤城县| 塘沽区| 芮城县| 三穗县| 奉新县| 贡觉县| 尚义县| 多伦县| 宜兴市|