[WordPress] 外掛分享: Inline JavaScript in Head

首頁外掛目錄 › Inline JavaScript in Head
⚠ 此外掛已下架 — 不再更新維護,建議勿安裝。
10+
安裝啟用
☆☆☆☆
1/5 分(1 則評價)
43 天前
最後更新
問題解決
WordPress 4.0+ PHP 5.4+ v1.2.1 上架:2019-03-10

內容簡介

此外掛已被退休且不再維護,建議停用並移除。若需在頁面頭部嵌入 JavaScript,應直接在程式碼中撰寫,而非透過外掛。

【主要功能】
• 提供過時的 JavaScript 嵌入功能
• 不再維護,建議使用其他方法
• 支援直接在程式碼中撰寫 JavaScript

外掛標籤

開發者團隊

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

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

原文外掛簡介

This plugin is retired and no longer maintained. Please deactivate and remove it.
Since 2020 this readme pointed at Embed JavaScript File Content as the successor.
That plugin has now been retired as well, so this notice was sending you to a dead
end. There is no successor plugin — here is what to do instead.
What to do instead
If you wrote the script yourself, do not route a file through a plugin — write the
code inline in the first place. WordPress has supported that since 4.5:
wp_add_inline_script( 'my-handle', 'if (!("localStorage" in window)) { /* ... */ }', 'before' );

For code that has to run before anything paints, print it in wp_head directly.
Both are simpler, and neither breaks a Content Security Policy the way an inlined
file does — this plugin sets neither a nonce nor a hash, so a strict script-src
blocks its output.
For a script you do not control, dequeue it and re-add its content yourself:
add_action( 'wp_enqueue_scripts', function () {
$handle = 'some-foreign-handle';
$src = wp_scripts()->registered[ $handle ]->src ?? null;
if ( ! $src ) {
return;
}
$path = ABSPATH . ltrim( wp_make_link_relative( $src ), '/' );
if ( ! is_readable( $path ) ) {
return;
}
wp_dequeue_script( $handle );
wp_add_inline_script( 'some-handle-you-own', file_get_contents( $path ) );
}, 20 );

You decide there how the file is located, which is the part this plugin got wrong:
it resolved the path relative to the current working directory, so the result
depended on which entry script served the request.
Why it is being retired
The benefit it was written for has largely gone. Under HTTP/1.1 every file cost a
round trip, so inlining a small critical script was a real win. HTTP/2 and HTTP/3
multiplex requests and removed most of that cost, while inlining still gives up
browser caching — the code travels with every HTML response instead of being cached
once.
How it used to work
In some cases you cannot wait for a JavaScript file to load, even if it is placed early in the section of your template. You can benefit from better performance, if you place the JavaScript code directly inside a

文章
Filter
Mastodon