是否从WordPress主页中删除Css&JS文件以提高页面速度?

时间:2013-11-05 作者:muhammad usman

参考https://stackoverflow.com/questions/12463304/wordpress-load-plugin-css-js-in-functions-php-w-conditional-tags 由@maiorano84回答

我正在使用cforms, FancyBox, WP Page Numbers 插件Wordpress. 从…起Page Speed 从我的角度来看,我想从所有页面中删除这些插件的CSS和JS,除了单个贴子页面。我还想把JS脚本放在页脚中。下面是我一直在尝试的代码,但运气不好。有人能帮我吗?

if ( !is_single() ) {
add_action( \'wp_print_styles\', \'my_deregisters\', 100 );
add_action( \'wp_print_scripts\', \'my_deregisters\', 100 );

    function my_deregisters() {
        remove_action(\'wp_head\', \'wp_page_numbers_stylesheet\');
        wp_deregister_style( \'fancybox\' ); 
        wp_deregister_script( \'fancybox\' ); 
        wp_dequeue_script(\'fancybox\');      
        wp_dequeue_style(\'fancybox\'); 
        remove_action(\'wp_print_scripts\', \'mfbfw_load\');
        remove_action(\'wp_print_styles\', \'mfbfw_css\');
        remove_action(\'wp_head\', \'mfbfw_init\');
        remove_action(\'wp_head\', \'cforms_style\');

        }

} else {  }

1 个回复
最合适的回答,由SO网友:helgatheviking 整理而成

如果将它添加到包含条件的挂钩中,效果会更好吗?

function my_deregisters() {

  if ( !is_single() ) {
   remove_action(\'wp_head\', \'wp_page_numbers_stylesheet\');
   //wp_deregister_style( \'fancybox\' ); // i think this can be removed
   //wp_deregister_script( \'fancybox\' ); // i think this can be removed
   wp_dequeue_script(\'fancybox\');      
   wp_dequeue_style(\'fancybox\'); 
   remove_action(\'wp_print_scripts\', \'mfbfw_load\');
   remove_action(\'wp_print_styles\', \'mfbfw_css\');
   remove_action(\'wp_head\', \'mfbfw_init\');
   remove_action(\'wp_head\', \'cforms_style\');
 }

} 
add_action( \'wp_enqueue_scripts\', \'my_deregisters\', 100 );

结束