[WordPress] 外掛分享: Scripts To Footer

WordPress 外掛 Scripts To Footer 的封面圖片。

前言介紹

  • 這款 WordPress 外掛「Scripts To Footer」是 2013-01-26 上架。
  • 目前有 8000 個安裝啟用數。
  • 上一次更新是 2025-11-17,距離現在已有 101 天。
  • 外掛最低要求 WordPress 5.3 以上版本才可以安裝。
  • 外掛要求網站主機運作至少需要 PHP 版本 7.4 以上。
  • 有 37 人給過評分。
  • 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。

外掛協作開發者

joshuadnelson |

外掛標籤

head | speed | footer | javascript | performance |

內容簡介

n

這個小型外掛程式可以把腳本搬至頁面底部的 JavaScript 塊中。請注意,您必須擁有使用wp_enqueue_scripts正確的外掛和佈景主題來實現此目的。

您可以通過所需的文章/頁面編輯螢幕 metabox 直接停用此外掛。

您可以透過設定頁面在特定存檔頁面(部落格頁面、搜尋頁面、文章類型和分類檔案)停用此外掛。

出了問題了嗎?嘗試通過 "設定 > 移動腳本至底部 > "將jQuery放回頁首的選框中,重新啟用頁首腳本。如果沒有作用,請參考以下步驟,使用 stf_exclude_scripts 過濾器來解決問題。

請在 GitHub 上檢查這個外掛的 說明文檔或以下的簡易步驟。

將特定腳本保留在頁首

從版本 0.6 開始,您現在可以在頁首保留特定腳本。注意:這將列印出它所依賴的任何腳本(如果您想在頁首保留jquery-effects-core,您也會得到頁首的jQuery,因此無需添加兩個)。

對於 jQuery,請查看設定頁面選項,因為它是一個普遍的請求,因此我們將其內置到設定中。

對於任何其他腳本,使用此過濾器:

add_filter('stf_exclude_scripts', 'jdn_header_scripts', 10, 1);
function jdn_header_scripts($scripts)
{
    $scripts[] = 'backbone'; //將'backbone'替換為腳本slug
    return $scripts;
}

您需要正確的腳本slug,該slug在腳本註冊時使用,當該腳本已排隊時,該腳本將僅被列印到頁首。查看隨WordPress「開箱即用」註冊的腳本。

注意:從版本 0.6.3開始,條件標記將與過濾器 stf_exclude_scripts 可使用。

自訂文章類型支援

如果您熟悉代碼,則可以使用 scripts_to_footer_post_types 過濾器來更改套用此方法的文章類型(預設僅套用於頁面和文章)。例如,如果您有一個名為"project"的自訂文章類型,您可以使用以下方式通過文章類型過濾器添加對此 metabox 的支援:

function stf_add_cpt_support($post_types) {
     $post_types[] = 'project';

     return $post_types;
}
add_filter('scripts_to_footer_post_types', 'stf_add_cpt_support');

通過過濾器排除頁面/文章/樣板

從版本 0.5 開始,您可以使用核取方塊選項在特定頁面/文章停用插件的操作,或者使用過濾器(從版本 0.6 開始更新)。該過濾器還傳遞了文章/頁面ID,如果有的話(存檔模板沒有id!)。

例如,對於"頁面"文章類型:

function stf_exclude_my_page($exclude_page, $post_id) {
    if( is_front_page() ) {
        $exclude_page = 'on'; //這將打開 "排除" 選項
    }
    返回 $exclude_page;
}
add_filter('stf_page', 'stf_exclude_my_page');

將 stf_page 替換為 stf_post 以獲取文章,或使用您的自訂文章類型的 slug。例如,名為"project"的文章類型可以使用 stf_project 過濾器過濾。

更多文檔

原文外掛簡介

This small plugin moves scripts to the footer. Note that this only works if you have plugins and a theme that utilizes wp_enqueue_scripts correctly.
You can disable the plugin on specific pages and posts directly via the post/page edit screen metabox.
You can disable the plugin on specific archive pages (blog page, search page, post type and taxonomy archives) via the settings page.
Everything Broken? Try placing jQuery back into the header via Settings > Scripts to Footer, “Keep jQuery in the Header” checkbox. If that doesn’t work, refer to the walkthrough below for using the stf_exclude_scripts filter for the script that is causing the issue.
Check out the documentation on GitHub or some quick walkthroughs below.
Keeping specific Scripts in the Header
As of version 0.6 you can now keep specific scripts in the header. Note: this will print any scripts they depend on as well (if you want to keep jquery-effects-core in the header, you’ll also get jQuery in the header, so no need to add both).
Specifically for jQuery, see the settings page option, as it is a common request we’ve built it into the settings.
For any other scripts, use this filter:
add_filter( 'stf_exclude_scripts', 'jdn_header_scripts', 10, 1 );
function jdn_header_scripts( $scripts ) {

$scripts[] = 'backbone'; // Replace 'backbone' with the script slug

return $scripts;
}

You will need the correct script slug, which is used when the script is registered, and the script will only be printed into the header if it’s enqueued. Check out the scripts that come registered out-of-the-box with WordPress.
Note: As of version 0.6.3, conditional tags will work with the stf_exclude_scripts filter.
Custom Post Type Support
If you’re comfortable with code you can use the scripts_to_footer_post_types filter to change the post types this applies to (it only applies to pages and posts by default). For example, if you have a custom post type called “project” you could add support for this metabox via the post type filter like this:
function stf_add_cpt_support( $post_types ) {

$post_types[] = 'project';

return $post_types;
}
add_filter( 'scripts_to_footer_post_types', 'stf_add_cpt_support' );

Excluding Pages/Posts/Templates Via Filter
You can either use the checkbox option to disable the plugin’s action on a specific page/post, or you can utilize a filter. The filter also passes the post/page id, if there is one (archive templates don’t have ids!).
For example, for the “page” post type:
function stf_exclude_my_page( $exclude_page, $post_id ) {

if ( is_front_page() ) {
$exclude_page = 'on'; // this turns on the "exclude" option
}

return $exclude_page;
}
add_filter( 'stf_page', 'stf_exclude_my_page' );

Replace stf_page with stf_post for posts, or the slug of your custom post type. For instance, a post type called “project” can be filtered with stf_project.
More Documentation
See the plugin’s wiki.
Development / Contributing
View this plugin on GitHub.
Support
Please feel free to open a Github Issue to report conflicts or goto the WP.org support forum. If there is something wrong with Scripts-to-Footer, we’ll update it. However, if it’s a another plugin or theme we can only contact the developer with the issue to attempt to resolve it.

各版本下載點

  • 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
  • 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「Scripts To Footer」來進行安裝。

(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。


0.1 | 0.2 | 0.3 | 0.5 | 0.6.0 | 0.6.1 | 0.6.2 | 0.6.3 | 0.7.0 | 0.7.1 | 0.7.2 | 0.7.3 | trunk | 0.6.4.1 |

延伸相關外掛(你可能也想知道)

  • Shortcoder — Create Shortcodes for Anything 》Shortcoder 外掛可讓您建立 HTML、JavaScript、CSS 和其他代碼片段的自訂捷徑。現在,這些捷徑可以在文章/頁面中使用,並且該片段將取代其位置。, ✍ 輕鬆建立...。
  • Enable jQuery Migrate Helper 》隨著 WordPress 5.5 的更新,一個名為 jquery-migrate 的遷移工具不再默認啟用。這可能會導致一些主題或外掛在運行舊代碼時缺少功能或出現意外行為。, 此外掛...。
  • Async JavaScript 》使用 Async Javascript 解決 above-the-fold 內容的 Render-blocking Javascript 問題。, Render-blocking Javascript 會導致 above-the-fold 內容在 Javascr...。
  • Speculative Loading 》- 這個 WordPress 外掛支援 Speculation Rules API,該 API 可以根據使用者互動,定義規則來動態預取或預渲染特定的 URL。, - 請參閱 Speculation Rules WICG...。
  • Scripts n Styles 》這個外掛讓管理員用戶能夠將自訂的 CSS 和 JavaScript 直接添加到個別文章、頁面或任何註冊的自訂文章類型中,也可以將類別添加到 body 標籤和文章容器中。還...。
  • Web Worker Offloading 》**總結:**, 這個外掛將 JavaScript 執行工作委派給 Web Worker,在釋放主執行緒的同時提高了效能。這將導致互動至下一次繪製(INP)分數的提升。值得注意的...。
  • Theme Authenticity Checker (TAC) 》掃描所有主題文件,尋找潛在的惡意或不需要的程式碼。, TAC 是什麼, TAC 代表主題真實性檢查器。TAC 搜尋每個安裝主題的原始檔案,尋找惡意程式碼的跡象。如...。
  • Code Embed 》注意: WordPress 5.0 及以上版本的使用者請查看常見問題以了解如何在區塊編輯器中使用此外掛。, Code Embed 外掛允許您在文章中嵌入程式碼(JavaScript 和 H...。
  • SOGO Add Script to Individual Pages Header Footer 》已經在 Gutenberg 上進行測試, 創建一個簡單的方式,為個別頁面、文章或自訂文章類型的標題和頁腳添加 JavaScript 代碼,, 例如: 在感謝頁面上添加轉換代碼, ...。
  • jQuery Updater 》此外掛更新jQuery至最新的官方穩定版本,該版本通常不會在WordPress最新的穩定版本中提供。, jQuery Migrate也包含在內,以實現向下相容性。, 不會替換任何檔...。
  • BJ Lazy Load 》延遲載入可使您的網站加載速度更快,並節省頻寬。, 此外掛可取代您所有的文章圖片、文章縮略圖、大頭貼圖片和內容的 iframe,並使用佔位圖片,當訪客滾動頁面...。
  • Use Google Libraries 》e's content distribution network (CDN) URLs for the supported libraries., This hook is used to replace the default WordPress registered script sour...。
  • Raw HTML 》is capability is only granted to administrators. If you’re not an admin, you will need to ask them to add it to your role., Raw HTML is not r...。
  • Custom CSS and JavaScript 》這款外掛可以讓您在 WordPress 網站中添加自訂全站 CSS 樣式和 JavaScript 代碼,有助於覆蓋佈景主題樣式和添加客戶端功能。功能包括:, , 代碼編輯器,帶有...。
  • CSS & JavaScript Toolbox 》感謝您查看我們的程式碼片段外掛。我相信它將完全符合您的需求。, 適用於以下情況:, , 前端修改(無需修改主題文件), 添加功能(無需修改functions.php), ...。

文章
Filter
Mastodon