去掉无用的 WordPress 样式

今天查看源码是发现头部出现一条自动添加的inline样式,“.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}”,如图所示:

去掉无用的Wordpress inline样式

起初以为是插件添加的,关闭所有插件后该东西依然存在。后来查到原来是WordPress自动添加的,recentcomments,应该就是为调用最新评论的边栏插件应用的样式,由于我没有使用自带的评论调用,所以这对于我来说就是无用的,于是就设法去掉它。

方法一:修改/wp-includes/default-widgets.php中的以下代码。

function recent_comments_style() {
	if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
		|| ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) )
		return;
	?>
<style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
<?php
}

从注释可以看出这是WordPress模板的hack,这段东西要怎么修改要斟酌斟酌了,直接去掉里面“style”之间的东西就最简单了。这个方法有个缺点就是升级之后继续修改……

方法二:添加一下带到到模板的function.php中。

function twentyten_remove_recent_comments_style() {  
	global $wp_widget_factory;  
	remove_action( 'wp_head', array( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style' ) );  
}  
add_action( 'widgets_init', 'twentyten_remove_recent_comments_style' );