前言介紹
- 這款 WordPress 外掛「Headless Converter」是 2021-10-07 上架。
- 目前尚無安裝啟用數,是個很新的外掛。如有要安裝使用,建議多測試確保功能沒問題!
- 上一次更新是 2022-01-28,距離現在已有 1190 天。超過一年沒更新,安裝要確認版本是否可用。以及後續維護問題!
- 外掛最低要求 WordPress 5.6 以上版本才可以安裝。
- 外掛要求網站主機運作至少需要 PHP 版本 7.4 以上。
- 尚未有人給過這款外掛評分。
- 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。
外掛協作開發者
attlii |
外掛標籤
內容簡介
當滿足特定條件時,將前端轉換為 JSON 響應。
靈感來源
在創建大量 WordPress + Web 應用程序時,我想找到一個標准且全面的解決方案,用於在 WordPress 內部為 Web 應用程序提取每個頁面的信息。內建的 REST API 在基本情況下運作良好,但它不支援路徑查詢。通常,這意味著對於每個項目,開發人員都會創建自定義 REST 端點,該端點將使用內容類型和 slug 參數返回所需內容。
WordPress 具有幾個內建函數來透過路徑檢索內容,url_to_postid 和 get_page_by_path,但它們似乎無法處理多語言插件、分類或存檔頁面,這意味著 WP 沒有一個可靠的方法透過 REST API 檢索內容。
此外採用 JSON 格式輸出,相比上述解決方案更為優秀,並且能夠通過應用密碼(WordPress v5.6 功能)和篩選器(讓開發人員更改發出的數據)增加一層安全性。
如何開始使用此外掛?
在 WordPress 環境中安裝並啟用此外掛。
為擁有管理員角色的用戶創建應用程序密碼。
完成以上步驟後,使用添加身份驗證標題 的方式對頁面進行請求。有關更多幫助,請參閱本文檔末尾的數據提取示例。
修改輸出內容
插件輸出當前頁面的 Post 對象或 null。這可以使用 headless-converter-modify-data 篩選器進行修改。您可以修改傳遞的文章對象或在下面示例中進行自己的邏輯。
/**
* 修改 Headless Converter 外掛的輸出。
*
* @param WPPost | null $post - 當前模板的文章對象
*/
function modify_headless_converter_output($post) {
if(is_404()) {
return "此區塊渲染 404 頁面內容";
} else if(is_page()) {
return "此區塊渲染頁面文章類型內容";
} else if (is_singular('post')) {
return "此區塊渲染單一文章內容";
} else if(is_home()) {
return "此區塊渲染文章存檔";
}else {
return $post;
}
}
add_filter('headless-converter-modify-data', 'modify_headless_converter_output');
數據提取示例
Fetch
const username = "admin"
const password = "1111 1111 1111 1111 1111"
const url = "http://localhost:3000/?page_id=2"
const opts = {
headers: {
'Authorization': 'Basic ' + btoa(username + ":" + password)
},
}
fetch(url, opts).then(r => r.json()).then(console.log)
Axios
const axios = require("axios")
const username = "admin"
const password = "1111 1111 1111 1111 1111"
const url = "http://localhost:3000/?page_id=2"
const opts = {
auth: {
username,
password
}
}
axios(url, opts).then(r => r.data).then(console.log)
原文外掛簡介
Converts frontend to JSON response when request is done with certain conditions.
Inspiration
After creating a bunch of headless WordPress + Web app stacks, I wanted to find a standard and a all-round solution for fetching per page information for web applications inside WordPress. Built-in rest api works fine in basic cases, but it doesn’t support querying by path. Usually this meant that for each project developers would create a custom rest endpoint which would return expected content using content type and slug parameters.
WordPress has few built-in functions to retrieve content by path, url_to_postid and get_page_by_path, but they don’t seem to work with multilanguage plugins, taxonomy or archive pages, which means that WP doesn’t have a reliable way to fetch content this way through rest api.
This plugin converts frontend to JSON which seems after above solution the best way to do things, with added layer of security through application passwords (WordPress v5.6 feature) and a filter, which let’s developers alter outgoing data.
How to start using the plugin
Install and activate this plugin in your environment
Create application password for a user with administrator role
After above steps have been made, make a request to a page with added Authorization header. See data fetching examples at the end of this documentation for more help
Modifying the output
Plugin outputs current page’s Post object or null. This can be modified using headless-converter-modify-data-filter. You can either modify passed in post object or do your own logic like in the example below.
/**
* Modifies Headless Converter plugin's output.
*
* @param WPPost|null $post - Current template's post object
*/
function modify_headless_converter_output($post) {
if(is_404()) {
return "this block renders 404 page content";
} else if(is_page()) {
return "this block renders page post types content";
} else if (is_singular('post')) {
return "this block renders single post content";
} else if(is_home()) {
return "this block renders post archive";
} else {
return $post;
}
}
add_filter('headless-converter-modify-data', 'modify_headless_converter_output');
Data fetching examples
Fetch
const username = "admin"
const password = "1111 1111 1111 1111 1111"
const url = "http://localhost:3000/?page_id=2"
const opts = {
headers: {
'Authorization': 'Basic ' + btoa(username + ":" + password)
},
}
fetch(url, opts).then(r => r.json()).then(console.log)
Axios
const axios = require("axios")
const username = "admin"
const password = "1111 1111 1111 1111 1111"
const url = "http://localhost:3000/?page_id=2"
const opts = {
auth: {
username,
password
}
}
axios(url, opts).then(r => r.data).then(console.log)
各版本下載點
- 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
- 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「Headless Converter」來進行安裝。
(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。
1.0.0 | 1.0.1 | 1.0.2 | 1.0.3 | 1.0.4 | 1.0.5 | 1.0.6 | 1.0.7 | 1.0.8 | trunk |
延伸相關外掛(你可能也想知道)
WPGraphQL 》WPGraphQL 是一個免費、開源的 WordPress 外掛,為任何 WordPress 網站提供可延伸的 GraphQL schema 和 API。, 以下是一些有助於你開始使用 WPGraphQL 的連結...。
WPGraphQL for ACF 》- WPGraphQL for Advanced Custom Fields 是一個免費的開源 WordPress 外掛,將 ACF Fields 和 Field Groups 添加到 WPGraphQL Schema 中。, - 創建 ACF Fiel...。
Headless Mode 》Headless 模式為所有嘗試訪問網站的用戶設置重定向。僅允許訪問 REST API、WP GraphQL API 或任何已登錄的用戶嘗試訪問無頭安裝以編輯或創建文章的請求。。
Faust.js 》 Faust 外掛與 Faust NPM 套件 協同工作,透過 GraphQL 調變和 REST API 端點,讓解耦式前端與 WordPress 進行驗證,並扮演 Faust 前端應用程式與 WordPress ...。
Headless WooCommerce Made Easy with CoCart 》重要訊息, 自 2022 年 10 月 5 日起,WordPress.org 中的 CoCart 外掛將不再進行重大更新,此舉是為了專注於付費的 CoCart Pro 版本。只有針對錯誤的次要修復...。
CoCart CORS Support 》這款免費的外掛程式為 CoCart 提供支援 CORS,允許 CoCart 在多個網域上運作。, 只需安裝並啟用,無需任何配置!, 工具和庫, , CoCart Beta Tester 讓您輕鬆...。
WPGraphQL Blocks 》總結:透過wp-graphql外掛,可將Gutenberg塊轉換成JSON資料。, , 問題與答案:, , 問題:wp-graphql是什麼外掛?, 答案:wp-graphql是在WordPress上使用Graph...。
WP Headless 》登入的使用者會被重新導向到文章的編輯頁面,而未登入的使用者必須先登入。這表示您可以根據您的永久連結設定分享一個可讀取的連結直接到編輯器。, 對於任務...。
CoCart – Cart API Enhanced 》此免費 WordPress 外掛是針對 CoCart 的附加元件,可增強傳回給購物車和添加到其中的商品的資料。, 功能, 每個添加到購物車的商品都會傳回以下資料:, , 返回...。
WPGraphQL IDE 》總結:WordPress 外掛 GraphQL IDE for WPGraphQL 為使用者提供一個更直觀的界面,方便進行 GraphQL 查詢和測試。, , 問題與答案:,
- ,
CoCart JWT Authentication 》- CoCart的免費附加元件可讓您透過簡單的JWT Token進行身份驗證。- 啟用PHP HTTP授權標頭- 大多數共享主機默認已禁用HTTP授權標頭。- 要啟用此選項,您需要通...。
Publish to Netlify 》使用 WordPress 作為後端,輕鬆將靜態網站部署到 Netlify。此外掛使用 Netlify webhook 觸發部署流程,建構您的靜態網站。。
WP Headless CMS Framework 》該外掛提供一個框架,將 WordPress 設置為無頭 CMS。, 框架選項包括:, , JSON Web Token 身份驗證(可選), 將 Gutenberg 區塊數據添加到 REST 響應中(可選...。
WUXT Headless WordPress API Extensions 》此外掛會為 WordPress Rest API 添加一些擴充功能,旨在使 WordPress 作為 Headless CMS 更易於使用。, 它原本是為 WUXT 設計的,WUXT 是一個帶有 NuxtJs 的 ...。
Gato GraphQL 》- Gato GraphQL 是一個能夠與 WordPress 網站的資料互動的工具,可以檢索、操作和再次儲存任何您所需的資料, - Gato GraphQL 可以處理來自多個外掛的功能:, ...。