[WordPress] 外掛分享: Headless Converter

首頁外掛目錄 › Headless Converter
全新外掛
安裝啟用
尚無評分
1517 天前
最後更新
問題解決
WordPress 5.6+ PHP 7.4+ v1.0.6 上架:2021-10-07

內容簡介

當滿足特定條件時,將前端轉換為 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)

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.0.6) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「Headless Converter」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

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)

延伸相關外掛

文章
Filter
Apply Filters
Mastodon