不删除wp_head()
或wp_footer()
因为它们是触发其他挂钩所必需的。相反,根据页面类型、页面ID、页面模板、帖子类别等有条件地删除脚本/样式表。
我认为你最需要担心的是网站首页上的这一点,因为你的所有网站CSS和主要JavaScript都将加载到该页面上,并缓存到网站的其余部分(是吗?)一旦人们下载了首页上的所有内容并缓存了各种资产,任何gzip脚本和样式表都应该可以很快加载到后面的任何页面上。
这是我去年做的一个网站上的一个漏洞,我在头版删除了一堆脚本和CSS。
// actions to remove unnecessary scripts and styles
add_action(\'wp_print_scripts\', \'wpse_84052_removeScripts\', 100);
add_action(\'wp_print_styles\', \'wpse_84052_removeStyles\');
/**
* performance: remove some scripts we don\'t need on splash page
*/
function wpse_84052_removeScripts() {
if (is_front_page()) {
// events manager
wp_dequeue_script(\'events-manager\');
wp_dequeue_script(\'events-manager-pro\');
wpse_84052_removeObjectFilters(\'wp_head\', \'EM_Pro\');
wpse_84052_removeObjectFilters(\'wp_head\', \'EM_Coupons\');
// NextGEN Gallery
wp_dequeue_script(\'ngg-slideshow\');
wp_dequeue_script(\'shutter\');
}
}
/**
* performance: remove some stylesheets we don\'t need on splash page
*/
function wpse_84052_removeStyles() {
if (is_front_page()) {
// WP Flexible Map plugin styles
wp_dequeue_style(\'flxmap\');
// NextGEN Gallery plugin styles
wp_dequeue_style(\'NextGEN\');
wp_dequeue_style(\'shutter\');
// Events Manager plugin styles
wp_dequeue_style(\'events-manager\');
// wp-category-posts-list plugin styles
wp_dequeue_style(\'wp_cpl_css_3\');
wp_dequeue_style(\'wp_cpl_css_2\');
wp_dequeue_style(\'wp_cpl_css_1\');
wp_dequeue_style(\'wp_cpl_css_0\');
wp_dequeue_style(\'wp-cpl-base-css\');
}
}
/**
* remove filters that are methods of an object of some class
* @param string $filterName name of action or filter hook
* @param string $className name of class for object method
*/
function wpse_84052_removeObjectFilters($filterName, $className) {
global $wp_filter;
// must take a variable to iterate over array of filters,
// else a subtle reference bug messes up the original array!
$filters = $wp_filter[$filterName];
foreach ($filters as $priority => $hooks) {
foreach ($hooks as $idx => $filter) {
// check for function being a method on a $className object
if (is_array($filter[\'function\']) && (is_a($filter[\'function\'][0], $className) || $filter[\'function\'][0] === $className)) {
remove_filter($filterName, $idx, $priority);
break;
}
}
}
}