內容簡介
此外掛已被退休且不再維護,建議停用並移除。若需在頁面頭部嵌入 JavaScript,應直接在程式碼中撰寫,而非透過外掛。
【主要功能】
• 提供過時的 JavaScript 嵌入功能
• 不再維護,建議使用其他方法
• 支援直接在程式碼中撰寫 JavaScript
外掛標籤
開發者團隊
原文外掛簡介
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
