[WordPress] 外掛分享: Only REST API

首頁外掛目錄 › Only REST API
30+
安裝啟用
尚無評分
3751 天前
最後更新
問題解決
WordPress 4.0+ v1.0.0 上架:2015-09-24

內容簡介

是否有一個 WordPress 安裝只作為您的網頁應用程式的資料層和/或管理介面?

此外掛將有效地關閉所有預設的前端輸出,重定向所有前端 URL 到主站 URL,並可選擇顯示您選擇的訊息。該訊息可以是一個簡單明了的純文字訊息,或者您可以使用包含的過濾器完全控制 HTML 輸出。

此外掛不會影響 wp-admin 中的所有活動和所有請求資源,例如圖像、腳本、檔案等。只有透過 template_redirect 動作的請求將受到影響。

訊息輸出

為了不汙染您可能在應用程式中使用的資料結構(例如文章和頁面),此外掛包含了一個設定頁面,其包含一個簡單的文字區塊,您可以在其中儲存任何您想要顯示的基本訊息。該訊息內容存儲於選項表格中,並且該文字區塊支援您可以在文章內容中使用的所有 HTML。

您可以使用 only_rest_api_page_content 過濾器在 <body> 標籤內包含任何自訂 HTML 輸出,或者您可以使用 only_rest_api_page_html 過濾器完全替換所有訊息頁面的 HTML 輸出,包括 <html> 和 <head> 標籤。

此外還有一個 only_rest_api_page_css 過濾器,允許您覆蓋此外掛包含的 5 行 CSS,以將訊息置中於頁面中。

如果您想要添加 hook 或有功能請求,請讓我知道。在 Github 上,我們歡迎 pull requests Github.

過濾器範例

在 <body> 標籤內使用自訂 HTML:

add_filter( 'only_rest_api_page_content', 'xxx_page_content' );
function xxx_page_content( $content ) {

$content = '<div class="custom-output">Sorry, I Only Speak REST. Please try again at a proper endpoint.</div>';

return $content;
}

將自訂 CSS 添加到預設輸出:

add_filter( 'only_rest_api_page_css', 'xxx_page_css' );
function xxx_page_css( $css ) {

$css .= '.page-content { color: red; font-size: 72px; }';

return $css;
}

替換訊息頁面的整個 HTML 輸出:

add_filter( 'only_rest_api_page_html', 'xxx_page_html' );
function xxx_page_html( $html ) {

ob_start();

?>
<!doctype html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Call me back over the REST API yo!</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="your-awesome-stylesheet.css">
<script type="text/javascript">
// Do neat stuff...
</script>
</head>
<body>
<div class="page-content">
Ain't nobody got time for non-REST API requests. Please try again at a proper endpoint. 🙂
</div>
</body>
</html>
<?php

$html = ob_get_clean();

return $html;
}

外掛標籤

開發者團隊

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

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

原文外掛簡介

Got a WordPress install that serves only as the data layer and/or admin UI of your web application?
This plugin will effectively turn off all default front end output, redirect all front end urls to the main site url, and optionally display a message of your choice. The message can be a simple plain text message or you can use the included filters to completely control the HTML output.
All activity in the wp-admin and all requests for assets like images, scripts, files, etc. will be unaffected by this plugin. Only requests that go through the template_redirect action will be affected.
Message Output
Rather than contaminate the data structures you might be using for your application (like posts and pages) this plugin includes a settings page with a simple textarea box where you can save any basic message you want to show. The message content is stored in the options table, and the textarea supports all the same HTML that you can use in post content.
You can use the only_rest_api_page_content filter to include any custom HTML output you want inside the tags, or you can use the only_rest_api_page_html filter to completely replace all HTML output of the message page, including the and tags.
There is also an only_rest_api_page_css filter that allows you to override the 5 lines of CSS this plugin includes to center the message on the page.
If you want a hook added or have a feature request let me know. Pull requests are welcome on Github.
Filter Examples
Use custom HTML inside the tags:
add_filter( 'only_rest_api_page_content', 'xxx_page_content' );
function xxx_page_content( $content ) {

$content = '

Sorry, I Only Speak REST. Please try again at a proper endpoint.

';

return $content;
}

Add custom CSS to the default output:
add_filter( 'only_rest_api_page_css', 'xxx_page_css' );
function xxx_page_css( $css ) {

$css .= '.page-content { color: red; font-size: 72px; }';

return $css;
}

Replace the entire HTML output for the page:
add_filter( 'only_rest_api_page_html', 'xxx_page_html' );
function xxx_page_html( $html ) {

ob_start();

?>





Call me back over the REST API yo!



Ain't nobody got time for non-REST API requests. Please try again at a proper endpoint. 🙂



延伸相關外掛

文章
Filter
Apply Filters
Mastodon