前言介紹
- 這款 WordPress 外掛「Genesis REST API Integration」是 2015-07-25 上架。
- 目前有 10 個安裝啟用數。
- 上一次更新是 2015-12-07,距離現在已有 3434 天。超過一年沒更新,安裝要確認版本是否可用。以及後續維護問題!
- 外掛最低要求 WordPress 4.0 以上版本才可以安裝。
- 有 1 人給過評分。
- 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。
外掛協作開發者
braad |
外掛標籤
api | rest | genesis | framework | integration |
內容簡介
如果您的網站使用 Genesis 框架,您很有可能使用 Genesis 的鉤子,例如 genesis_before_content 或 genesis_entry_footer。使用 WP REST API 時,預設情況下不會包含通過這些鉤子添加的任何內容。
此外掛程式篩選 API 回應以包含通過 Genesis 鉤子添加的任何內容。它將此內容添加到文章對象回應中,這意味著您無需進行單獨的請求或修改您的請求以獲取數據。只需像標準方式一樣要求文章,例如 /wp-json/wp/v2/posts/1,您就可以從 Genesis 鉤子獲得額外的內容。
示範差異,顯示啟用或停用該外掛程式時的回應
返回的 Genesis 鉤子內容是針對檢索到的特定文章的上下文生成的。上下文將與對於給定的文章、頁面或自定義文章類型的單個文章頁面相匹配。這意味著,如果您有以下代碼:
add_action( 'genesis_before_entry', 'mytheme_output_special_content' );
function mytheme_output_special_content() {
// Only on posts in a specific category
if ( in_category( '5' ) ) {
echo 'Some Content';
}
}
僅當文章實際上屬於類別為 5 時,回應對象上的 ‘genesis_before_entry’ 屬性的值為 ‘Some Content’。所有標准的條件語句,例如 is_archive()、is_single()、has_tag() 等都將得到尊重。
默認情況下,此外掛僅為具有輸出的鉤子添加回應對象中的屬性,但您可以使用篩選器修改此行為,例如:
add_filter( 'genesis_rest_api_return_empty_hooks', '__return_true' );
默認情況下,此外掛在文章、頁面和所有自定義文章類型中僅添加了額外的屬性,但您可以使用另一個篩選器指定要在哪些文章類型中包含額外數據:
add_filter( 'genesis_rest_api_supported_post_types', 'mytheme_genesis_rest_api_supported_post_types' );
function mytheme_genesis_rest_api_supported_post_types( $post_types ) {
// 只為電影自定義文章類型添加數據。
$post_types = array( 'movie' );
return $post_types;
}
注意:在 REST API 的 v2 版本中,自定義文章類型需要將額外屬性 show_in_rest、rest_base 和 rest_controller_class 設置在其文章類型對象上,以便 API 開始為其提供數據。此外我已經在此外掛程式中包含了一個篩選器,以添加這些屬性,因此您可以聲明對 CPT 的 REST API 支持並在一步中添加額外的 Genesis 數據,但默認情況下,該篩選器是關閉的,不會覆盖已設置的屬性(因為如果核心不假定您希望默認情況下所有 CPT 都可公開訪問,那麼我認為我也不應該這樣)。要打開此功能,請使用以下篩選器:
add_filter( 'genesis_rest_api_register_cpt_api_support', '__return_true' );
如果這樣做,您的 CPT 將在與文章類型對象上的官方名稱匹配的路徑上可用,因此,如果文章類型為 ‘movie’,而您有一部 ID 為 8 的電影,則該電影可以在 /wp-json/v2/movie/8 上訪問。最好將路由與核心公約 /posts/ 和 /pages/ 相匹配,因此,如果您想將路徑設置為 /movies/,而不是 /movie/,您只需特別設置 rest_base 屬性即可:
add_action( 'init', 'mytheme_change_cpt_routes', 11 );
funct
原文外掛簡介
If your site uses the Genesis framework, it’s likely that you’re making use of Genesis hooks like genesis_before_content or genesis_entry_footer. When using the WP REST API, any content that has been added to these hooks is not included in the response by default.
This plugin filters the response from the API to include any content added via Genesis hooks. It adds this content to the post object response, which means you don’t have to do a separate request or modify your request to get the data. Simply ask for a post in the standard way like /wp-json/wp/v2/posts/1 and you’ll get the extra content from the Genesis hooks.
Example diff showing the response with and without the plugin active
The Genesis hook content that is returned is generated with respect to the context of the specific post being retrieved. The context will match what it would be on the single post page for the given post, page, or custom post type. This means that if you’ve got some code like this:
add_action( 'genesis_before_entry', 'mytheme_output_special_content' );
function mytheme_output_special_content() {
// Only on posts in a specific category
if ( in_category( '5' ) ) {
echo 'Some Content';
}
}
You’ll get back ‘Some Content’ as the value of the property ‘genesis_before_entry’ on the response object only if the post is actually in category 5. All of the standard conditionals like is_archive(), is_single(), has_tag(), etc. will be respected.
By default this plugin only adds properties to the response object for hooks that have output, but you can modify this behavior using a filter like so:
add_filter( 'genesis_rest_api_return_empty_hooks', '__return_true' );
By default this plugin adds the extra properties to posts, pages, and all custom post types, but you can specify which post types you want to include the extra data with another filter:
add_filter( 'genesis_rest_api_supported_post_types', 'mytheme_genesis_rest_api_supported_post_types' );
function mytheme_genesis_rest_api_supported_post_types( $post_types ) {
// Only add data to the movie post type.
$post_types = array( 'movie' );
return $post_types;
}
NOTE: In v2 of the REST API, custom post types need to have the extra properties show_in_rest, rest_base, and rest_controller_class set on their post type objects in order for the API to start serving data for them. I’ve included a filter in this plugin for adding these properties so that you can declare REST API support for CPTs and add the extra Genesis data in one step, but the filter is off by default and won’t override properties that are already set (because if core doesn’t assume you want all your CPTs publicly accessible by default, I don’t think I should either). To turn this functionality on, use the included filter like this:
add_filter( 'genesis_rest_api_register_cpt_api_support', '__return_true' );
If you do this, you’re CPTs will be available at routes that match the official name of the post type found on the post type object, so if your post type is ‘movie’ and you have a movie with an id of 8, the movie will be accessible at /wp-json/v2/movie/8. It’s probably a better idea to match the core convention of /posts/ and /pages/, so if you want to make the route available at /movies/ instead of /movie/ you just need to specifically set the rest_base property like so:
add_action( 'init', 'mytheme_change_cpt_routes', 11 );
function mytheme_change_cpt_routes() {
global $wp_post_types;
$wp_post_types['movie']->rest_base = 'movies';
}
Here’s the full list of all the Genesis hooks that are currently supported:
genesis_before
genesis_before_header
genesis_site_title
genesis_site_description
genesis_header_right
genesis_after_header
genesis_before_content_sidebar_wrap
genesis_before_content
genesis_before_loop
genesis_before_while
genesis_before_entry
genesis_entry_header
genesis_before_entry_content
genesis_entry_content
genesis_after_entry_content
genesis_entry_footer
genesis_after_entry
genesis_after_endwhile
genesis_after_loop
genesis_before_sidebar_widget_area
genesis_after_sidebar_widget_area
genesis_after_content_sidebar_wrap
genesis_before_footer
genesis_footer
genesis_after_footer
genesis_after
And naturally, there is a filter to control which hooks are supported:
add_filter( 'genesis_rest_api_supported_hooks', 'mytheme_genesis_rest_api_supported_hooks' );
function mytheme_genesis_rest_api_supported_hooks( $genesis_hooks ) {
// Only include certian hooks.
$genesis_hooks = array(
'genesis_before_entry',
'genesis_after_entry',
);
return $genesis_hooks;
}
NOTE: The hooks genesis_header and genesis_loop are not included by default because they mostly call other hooks that are included, but you can always add them back in using the genesis_rest_api_supported_hooks filter.
NOTE: Returning formatted HTML over the REST API is not the best way to make use of a REST API to build a website. It would be preferable to return only the raw object data and build all of your HTML on the client side using the object data. With this plugin you can do exactly this with a little help from json_encode:
add_action( 'genesis_before_entry_content', 'mytheme_pass_array' );
function mytheme_pass_array() {
if ( is_single( 124 ) ) {
$json = array(
'a_key' => 'some value',
'another_key' => 'another value',
);
echo json_encode( $json );
}
}
Or the object version:
add_action( 'genesis_after_entry_content', 'mytheme_pass_object' );
function mytheme_pass_object() {
if ( in_category( 2 ) ) {
$json = new stdClass();
$json->some_key = 'some value';
$json->another_key = 'another value';
echo json_encode( $json );
}
}
Passing arbitrary objects and arrays like this really opens up some interesting possibilities.
If you have any ideas for new features or find a bug, please open an issue on Github. Pull requests are also encouraged :).
各版本下載點
- 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
- 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「Genesis REST API Integration」來進行安裝。
(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。
延伸相關外掛(你可能也想知道)
InfiniteWP Client 》InfiniteWP 可讓使用者從自己的伺服器管理無限數量的 WordPress 網站。, 主要功能:, , 自行託管系統:位於您自己的伺服器上,完全受您控制, 一鍵更新所有網站...。
WP Consent API 》WP Consent API 是一個外掛,可以標準化插件之間同意的同意類別溝通。它需要使用 Cookie 標語插件和至少一個支援 WP Consent API 的其他插件。, , 有了這個插...。
Disable REST API 》這是最全面的 WordPress REST API 存取控制外掛!, 輕鬆安裝,安裝後不需要額外設定即可使用。只需上傳並啟用,整個 REST API 將無法被一般訪客存取。, 但如...。
Mailgun for WordPress 》Mailgun 是超過10,000名網站和應用程式開發人員信任的電子郵件自動化引擎,用於發送、接收和追踪郵件。藉助Mailgun強大的郵件API,開發人員可以更多時間建立...。
Make Connector 》Make 是一個視覺化平台,讓你可以在幾分鐘內設計、建立和自動化任何事情 - 從簡單的任務到複雜的工作流程。使用 Make,你可以在 WordPress 和數千個應用程式...。
ACF to REST API 》此 WordPress 外掛在WordPress REST API中提供了Advanced Custom Fields的端點, 詳細資訊請參閱GitHub:https://github.com/airesvsg/acf-to-rest-api/。
WordPress REST API Authentication 》WordPress REST API 預設是鬆散的端點,駭客可以通過這些端點遠程控制您的網站。 您不希望駭客可以透過 WordPress 登錄和 WordPress 註冊或任何其他端點來獲...。
Disable WP REST API 》這個外掛只有一個功能:禁用沒有登入 WordPress 的訪客使用 WP REST API。不需要任何設定。, 這個外掛只有 22 行短小的程式碼(少於 2KB)。因此它非常輕量、...。
WPGet API – Connect to any external REST API 》簡單地連接WordPress至外部API的最簡單方式。, , WPGetAPI 可輕鬆地從無限量的第三方 REST API 發送和接收資料,然後使用「Shortcode」或「Template Tag」格...。
WP REST Cache 》如果您在使用 WordPress REST API 時遇到速度問題,這個外掛將允許 WordPress 快取 REST API 的回應,使其更快。, 此外掛提供以下功能:, , 快取所有預設的 W...。
WP REST API Controller 》WP REST API Controller 外掛提供使用者易用的圖形化介面,使管理員可以切換 WordPress 核心和自訂文章類型以及分類法的可見性和端點,同時也可以自訂 API 回...。
WordPress REST API (Version 2) 》WordPress 正轉變成一個完整的應用程式框架,因此需要新的 API。這個專案的目的是創建一個易於使用、易於理解且經過良好測試的框架,用於創建這些 API,以及...。
WP REST API – OAuth 1.0a Server 》使用這個外掛,您可以將應用程式連接至您的 WordPress 網站,而不必透露您的密碼。, 此外掛使用 OAuth 1.0a 協議,允許委派授權,即允許應用程式使用一組次要...。
Contact Form to Any API 》, 「Contact form 7 to Any API」是一款強大的外掛,能夠將聯絡表單 CF7 的數據傳送至任何第三方服務。使用者可以透過這款外掛將數據發送到客戶關係管理(CRM...。
Envato Toolkit 》這個外掛包含一個三個檔案的程式庫和視覺化介面,用於驗證客戶購買代碼、獲取特定 Envato 用戶的詳細信息(國家、城市、總追隨者數量、總銷量、頭像),獲取...。