[WordPress] 外掛分享: Script Logic

首頁外掛目錄 › Script Logic
WordPress 外掛 Script Logic 的封面圖片
10+
安裝啟用
★★★☆☆
3/5 分(2 則評價)
4254 天前
最後更新
問題解決
WordPress 2.8+ v0.3 上架:2014-06-15

內容簡介

這個外掛會列出所有JavaScript和樣式表,並帶有一個控制欄,讓您可以控制只在實際需要它們的頁面中包含CSS和JavaScript文件。文本欄讓您可以使用 WP 的條件標籤或任何一般PHP代碼。

注意:您引入的腳本邏輯會直接被評估。有權管理選項的任何人都有權添加任何代碼,包括惡意且可能破壞性的代碼。如果需要,可以使用可選過濾器‘script_logic_eval_override’自己的代碼繞過EVAL。(參見其他說明。)

編寫邏輯代碼

‘邏輯’欄中的文本可以是完整的PHP代碼,應當在您需要載入腳本時返回‘true’。如果文本中沒有‘return’,則會在開始時添加隱式‘return’,並在末尾添加‘;’。(這只是為了使像is_home()這樣的單個語句更方便。)

基礎知識

充分利用WP自己的條件標籤。您可以使用以下方法改變和組合代碼:

! (NOT)來反轉邏輯,例如:!is_home()當這不是首頁時為TRUE。
|| (OR)來組合條件。當X或Y為真時,X OR Y為TRUE。
&& (AND)使條件更具體。當X和Y都為TRUE時,X AND Y為TRUE。

WP論壇上有很多很好的代碼示例,網上的WP網站上也有。但是WP Codex也充滿了可以改編的好範例,例如測試文章是否在后代分類中。

範例

is_home() ——僅網誌主頁
!is_page('about') —— 除了這個特定的 WP ‘page’ 外的所有地方
!is_user_logged_in() —— 當用戶未登錄時顯示
is_category(array(5,9,10,11)) —— 一個給定類別ID的類別頁面
is_single() && in_category('baked-goods') —— 單個文章位於此類別中的類別頁面
strpos($_SERVER ['HTTP_REFERER'], "google.com")!=false —— 從谷歌搜索中單擊進入時載入的腳本
is_category() && in_array($cat, get_term_children( 5, 'category')) —— 類別頁面是類別5的後代
global $post; return (in_array(77,get_post_ancestors($post))); —— WP頁面是第77頁子頁面
global $post; return (is_page('home') || ($post->post_parent=="13")); —— 首頁或第13頁的子頁面

注意,在有明確‘return’的情況下,會在末尾加上額外的‘;’。

‘script_logic_eval_override’過濾器

在為每個腳本評估Script Logic代碼之前,Script Logic代碼的文本會通過此過濾器。如果過濾器返回布林結果,則使用此結果來確定是否載入腳本。返回TRUE以載入。

外掛標籤

開發者團隊

⬇ 下載最新版 (v0.3) 或搜尋安裝

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

原文外掛簡介

This plugin lists all JavaScripts and Style sheets with a control field that lets you control CSS & JavaScript files to include only on the pages where you actually need them. The text field lets you use WP’s Conditional Tags, or any general PHP code.
NOTE: The script logic you introduce is evaluated directly. Anyone who has permission to manage options will have the right to add any code, including malicious and possibly destructive code. There is an optional filter ‘script_logic_eval_override’ which you can use to bypass the EVAL with your own code if needed. (See Other Notes).
Writing Logic Code
The text in the ‘Logic’ field can be full PHP code and should return ‘true’ when you need the script to load. If there is no ‘return’ in the text, an implicit ‘return’ is added to the start and a ‘;’ is added on the end. (This is just to make single statements like is_home() more convenient.)
The Basics
Make good use of WP’s own conditional tags. You can vary and combine code using:

! (NOT) to reverse the logic, eg !is_home() is TRUE when this is NOT the home page.
|| (OR) to combine conditions. X OR Y is TRUE when either X is true or Y is true.
&& (AND) to make conditions more specific. X AND Y is TRUE when both X is true and Y is true.

There are lots of great code examples on the WP forums, and on WP sites across the net. But the WP Codex is also full of good examples to adapt, such as Test if post is in a descendent category.
Examples

is_home() — just the main blog page
!is_page('about') — everywhere EXCEPT this specific WP ‘page’
!is_user_logged_in() — shown when a user is not logged in
is_category(array(5,9,10,11)) — category page of one of the given category IDs
is_single() && in_category('baked-goods') — single post that’s in the category with this slug
strpos($_SERVER['HTTP_REFERER'], "google.com")!=false — script to load when clicked through from a google search
is_category() && in_array($cat, get_term_children( 5, 'category')) — category page that’s a descendent of category 5
global $post; return (in_array(77,get_post_ancestors($post))); — WP page that is a child of page 77
global $post; return (is_page('home') || ($post->post_parent=="13")); — home page OR the page that’s a child of page 13

Note the extra ‘;’ on the end where there is an explicit ‘return’.
The ‘script_logic_eval_override’ filter
Before the Script Logic code is evaluated for each script, the text of the Script Logic code is passed through this filter. If the filter returns a BOOLEAN result, this is used instead to determine if the script should load. Return TRUE to load.

延伸相關外掛

文章
Filter
Apply Filters
Mastodon