WP REST API (WP API) 是一個WordPress插件,其用途是為 WordPress 核心添加一個 JSON REST API , 以便于像移動應(yīng)用之類的應(yīng)用與 WordPress 進(jìn)行交互。
WP-API 是可擴展的,并且具有齊備的文檔,如果你使用 WP REST API (WP API) ,你很可能會被授權(quán)問題所困擾。
WP REST API (WP API) 能讓你創(chuàng)建、編輯獲取文章(各種WordPress內(nèi)置的文章類型的文章以及自定義類型的文章)、創(chuàng)建、編輯和獲取用戶等,因此,它在某些情形下需要認(rèn)證(授權(quán)),比如創(chuàng)建和編輯操作,就絕對需要授權(quán)才行,否則,處理申請會被 WordPress 拒絕。
據(jù) WP REST API (WP API) 的認(rèn)證(授權(quán))文檔來看,認(rèn)證方式有三種:cookie、oauth和簡單認(rèn)證。本文記錄如何實現(xiàn)自定義認(rèn)證。
據(jù)WordPress 官方開發(fā)記錄顯示:這個插件很可能會在2015年4月22日發(fā)布的 WordPress 4.2 版本中加入到WordPress核心,那樣的話,授權(quán)方式可能會有所改變,但不會大變。
舉個簡單的例子: 某個用戶通過手機拍了一張照片,想上傳到某個啟用了WP REST API (WP API) 的 WordPress 網(wǎng)站,那么,就需要認(rèn)證了吧,那么,怎么做呢?
下面將說一種用于此種情形的認(rèn)證方式。
用自定義 filter hook
在該插件目錄 lib 下有個類文件 class-wp-json-server.php ,其中有這段兒:
/**
* Check the authentication headers if supplied
*
* @return WP_Error|null WP_Error indicates unsuccessful login, null indicates successful or no authentication provided
*/
public function check_authentication() {
/**
* Pass an authentication error to the API
*
* This is used to pass a {@see WP_Error} from an authentication method
* back to the API.
*
* Authentication methods should check first if they're being used, as
* multiple authentication methods can be enabled on a site (cookies,
* HTTP basic auth, OAuth). If the authentication method hooked in is
* not actually being attempted, null should be returned to indicate
* another authentication method should check instead. Similarly,
* callbacks should ensure the value is `null` before checking for
* errors.
*
* 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 );
}
基于上面的這個函數(shù)以及其被調(diào)用位置,我們可以加進(jìn)去一個hook,以確認(rèn)認(rèn)證是否成功:
/**
* WP JSON API 認(rèn)證檢查
* @param null
* @return boolean 是否認(rèn)證成功
* @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;
}
//獲取從應(yīng)用GET過來的用戶id、app_key和app_token,當(dāng)然了,你也可以只用一個去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,當(dāng)然了,你也可以自定義一種算法,
//$wp_key = get_user_meta( $user_id, 'app_key', true);
$wp_token = get_user_meta( $user_id, 'app_token', true);
//將從應(yīng)用客戶端獲取到的值與數(shù)據(jù)庫存儲的值進(jìn)行對比
if(
( $wp_token == $app_token )
// &&( $wp_key == $app_key )
){
return true;
}
return false;
}
add_filter('json_authentication_errors', 'coolwp_rest_api_auth_check');
結(jié)論
加入 rest api 的 WordPress 甚至可以讓你做一個在線支付網(wǎng)站,有了這組 api ,基于 WordPress 的原生安卓應(yīng)用和IOS應(yīng)用可以更好的與 WordPress 站點進(jìn)行交互。






文章很不錯,很好。
想調(diào)用什么直接用json_encode自己封裝下就行了,沒必要弄個插件。。。
怎樣自定義輸出特定內(nèi)容為json格式呢?比如特定鏈接,特定文字~
系統(tǒng)里面有插件的呀!你可以去下載wordpress官方的wp-json
這個插件很可能會在2015年4月22日發(fā)布的 WordPress 4.2 版本中加入到WordPress核心
——這真是個天大的好消息啊
又被推遲了,但是在計劃上,這是這個插件比別的強的原因
非常有用,收藏
不錯,學(xué)習(xí)了技術(shù)達(dá)人
??
這個插件前段時間一直在折騰,基本上實現(xiàn)了調(diào)用文章以及自定義字段,還有自定義文章類型。不過像評論、登錄等沒有辦法實現(xiàn)。當(dāng)時是想實現(xiàn)Android客戶端的,不過谷歌上搜索了很多沒有找到類似的開源客戶端,沒辦法實現(xiàn)。這個插件總的說國外應(yīng)用的比較多,國內(nèi)的話基本上沒有看到過用這個來做什么,當(dāng)然除了一些公司外。
有辦法實現(xiàn)的,不過OAuth太麻煩了,看你要用什么語言實現(xiàn)了:js/php/lua/java/o-c
知道可以實現(xiàn),不過技術(shù)不到家實現(xiàn)不了而已