問題描述
如何獲取所有排隊的樣式或腳本,然後一次取消註冊?
最佳解決方案
我希望你知道你在做什麼您可以使用 wp_print_styles 和 wp_print_scripts 動作鈎子,然後在其各自的鈎子中獲取全局 $ wp_styles 和 $ wp_scripts 對象變量。
“registered” 屬性列出了註冊的腳本,”queue” 屬性列出了上述兩個對象的隊列腳本。
清空腳本和樣式隊列的示例代碼。
function pm_remove_all_scripts() {
global $wp_scripts;
$wp_scripts->queue = array();
}
add_action('wp_print_scripts', 'pm_remove_all_scripts', 100);
function pm_remove_all_styles() {
global $wp_styles;
$wp_styles->queue = array();
}
add_action('wp_print_styles', 'pm_remove_all_styles', 100);
次佳解決方案
您也可以通過查找正在調用的處理程序來進行操作,搜索 wp_enqueue_style 或 wp_enqueue_script,您可以在 functions.php 上取消註冊
add_action( 'wp_print_styles', 'my_deregister_styles', 100 );
function my_deregister_styles() {
wp_deregister_style( 'some-css' );
}
add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript() {
wp_deregister_script( 'tutorials-js' );
wp_deregister_script( 'gsc_dialog' );
wp_deregister_script( 'gsc_jquery' );
}
Hameedullah 解決方案更好,但是如果您遇到問題,因為一些腳本沒有加載給上面的一個鏡頭。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。