
內容簡介
Encute 提供了一個流利、聲明式的 API,供站點擁有者操縱 WordPress、佈景主題和外掛程式將腳本和樣式灌注到他們的網站上。將事物移動到頁尾、推遲載入、完全移除資產。或者異步載入腳本,作為模組,或作為 nomodule!
以下是您如何使用外掛程式的範例:
<?php
use CWS\Encute\{ Plugin, Script, Style };
add_action(Plugin::class, function (Plugin $plugin) {
$isContactPage = fn () => is_page('contact');
Script::get('contact-form-7')->keepIf($isContactPage)->footer()->defer();
Style::get('contact-form-7')->keepIf($isContactPage)->footer()->defer();
Style::get(['mediaelement', 'wp-mediaelement'])->footer()->defer();
Style::get('material-icons')->defer();
Script::get('jquery')->remove();
});
Wrapper
始終在此包裝函式中執行程式碼:
add_action(\CWS\Encute\Plugin::class, function (\CWS\Encute\Plugin $encute) {
// 在這裡編寫您的程式碼。
});
如果 Encute 不可用,此包裝函式將無操作,並等待 Encute 可用來運行,並傳遞 Encute 的主要類實例。
流利性
無論是 Script::get() 還是 Style::get() 都會返回它們自己的實例,以及它們方法的所有調用,因此您可以鏈接您的調用。
Script
static CWS\Encute\Script::get(string $handle): CWS\Encute\Script - 為該 handle 獲取 Script 實例。
CWS\Encute\Script::module(): CWS\Encute\Script - 讓此腳本作為模組。
CWS\Encute\Script::noModule(): CWS\Encute\Script - 讓此腳本作為 nomodule。
CWS\Encute\Script::footer(): CWS\Encute\Script - 將腳本(及其整個依賴家族)發送到頁尾。
CWS\Encute\Script::async(): CWS\Encute\Script - 異步載入腳本。
CWS\Encute\Script::defer(): CWS\Encute\Script - 推遲腳本載入。
CWS\Encute\Script::remove(): CWS\Encute\Script - 移除腳本。
CWS\Encute\Script::removeIf(callable $callback): CWS\Encute\Script - 如果回調函式解析為 true,則刪除腳本。
CWS\Encute\Script::keepIf(callable $callback): CWS\Encute\Script - 如果回調函式解析為 true,則保留腳本(否則刪除)。
Style
static CWS\Encute\Style::get(string $handle): CWS\Encute\Style - 為該 handle 獲取 Style 實例。
CWS\Encute\Style::footer(): CWS\Encute\Style - 將樣式(及其整個依賴家族)發送到頁尾。
CWS\Encute\Style::defer(): CWS\Encute\Style - 推遲樣式載入。
CWS\Encute\Style::remove(): CWS\Encute\Style - 移除樣式。
CWS\Encute\Style::removeIf(callable $callback): CWS\Encute\Style - 如果回調函式解析為 true,則刪除樣式。
CWS\Encute\Style::keepIf(callable $callback): CWS\Encute\Style - 如果回調函式解析為 true,則保留樣式(否則刪除)。
外掛標籤
開發者團隊
原文外掛簡介
Encute provides a fluent, declarative API for site owners to manipulate the scripts and styles that WordPress, themes, and plugins shove onto their site. Move things into the footer, defer loading, remove assets entirely. Or load scripts async, or as modules, or as nomodule!
Here’s an example of how you could use the plugin:
is_page('contact');
Script::get('contact-form-7')->keepIf($isContactPage)->footer()->defer();
Style::get('contact-form-7')->keepIf($isContactPage)->footer()->defer();
Style::get(['mediaelement', 'wp-mediaelement'])->footer()->defer();
Style::get('material-icons')->defer();
Script::get('jquery')->remove();
});
Wrapper
Always run code in this wrapper:
add_action(\CWS\Encute\Plugin::class, function (\CWS\Encute\Plugin $encute) {
// Your code here.
});
This wrapper will be a no-op if Encute is not available, and it will both wait for Encute to be available to run, and pass you Encute’s main class instance.
Fluency
Both Script::get() and Style::get() return an instance of themselves, as do all calls to their methods, so you can just chain your calls.
Script
static CWS\Encute\Script::get(string $handle): CWS\Encute\Script — get a Script instance for that handle.
CWS\Encute\Script::module(): CWS\Encute\Script — make the script a module.
CWS\Encute\Script::noModule(): CWS\Encute\Script — make the script nomodule.
CWS\Encute\Script::footer(): CWS\Encute\Script — send the script to the footer (along with its entire dependency family).
CWS\Encute\Script::async(): CWS\Encute\Script — make the script async.
CWS\Encute\Script::defer(): CWS\Encute\Script — make the script defer.
CWS\Encute\Script::remove(): CWS\Encute\Script — remove the script.
CWS\Encute\Script::removeIf(callable $callback): CWS\Encute\Script — remove the script if the callback resolves as true.
CWS\Encute\Script::keepIf(callable $callback): CWS\Encute\Script — keep the script if the callback resolves as true (else remove it).
Style
static CWS\Encute\Style::get(string $handle): CWS\Encute\Style — get a Style instance for that handle.
CWS\Encute\Style::footer(): CWS\Encute\Style — send the style to the footer (along with its entire dependency family).
CWS\Encute\Style::defer(): CWS\Encute\Style — defer the style’s loading.
CWS\Encute\Style::remove(): CWS\Encute\Style — remove the style.
CWS\Encute\Style::removeIf(callable $callback): CWS\Encute\Style — remove the style if the callback resolves as true.
CWS\Encute\Style::keepIf(callable $callback): CWS\Encute\Style — keep the style if the callback resolves as true (else remove it).
