[WordPress] 外掛分享: Scripts To Footer

首頁外掛目錄 › Scripts To Footer
WordPress 外掛 Scripts To Footer 的封面圖片
8,000+
安裝啟用
★★★★
4.7/5 分(37 則評價)
143 天前
最後更新
問題解決
WordPress 5.3+ PHP 7.4+ v0.7.3 上架:2013-01-26

內容簡介

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 過濾器過濾。

更多文檔

外掛標籤

開發者團隊

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

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

原文外掛簡介

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.

延伸相關外掛

文章
Filter
Apply Filters
Mastodon