不用插件打造安全快速的WordPress
2010-09-22 10:10 by hackerzhou最近把博客优化了一下,有一些简单的功能(比如去除wordpress的版本信息,和插件/主题载入js/css时的版本信息)是不用插件就能搞定的,还有一些css/js改整合的整合,能压缩的压缩,也能减肥不少。
1. 修改wp-includes\class.wp-scripts.php和wp-includes\class.wp-scripts.php让其不要为插件和主题载入的css/js加?ver=xxx的版本信息。注释掉如下两行代码:
1 2 | if ( ! empty ( $ver ) ) $src = add_query_arg( 'ver' , $ver , $src ); |
2. 修改wp-includes\general-template.php,隐藏rsd link和wlwmanifest link,这两个接口是用来给第三方离线编辑器,比如Windows Live Writer用的,一般要是没有这个习惯的话可以去掉,注释掉如下两句话就可以了。
1 2 3 4 5 6 7 | function rsd_link() { //echo '<link rel="EditURI" type="application/rsd+xml" title="RSD" href="' . get_bloginfo('wpurl') . "/xmlrpc.php?rsd\" />\n"; } function wlwmanifest_link() { //echo '<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="'. get_bloginfo('wpurl') . '/wp-includes/wlwmanifest.xml" /> ' . "\n"; } |
3.修改wp-includes\theme.php,修改这个主要是因为我们优化主题的时候会删除css中的注释,而wordpress默认会读取style.css里面的一些信息作为该主题的属性,去除方法是找到get_theme_data函数,全部注释掉就可以了。
4.修改wp-comments-post.php,原因参见我写的 另类WordPress防垃圾评论方法,试验了几天发现用这个方法屏蔽垃圾评论还是很有效果的,一条垃圾评论都没出现,那些机器人果真没那么智能。
1 2 3 4 | $comment_author =(isset( $_POST [ 'hz-hc-arg0' ]))?trim( strip_tags ( $_POST [ 'hz-hc-arg0' ])):null; $comment_author_email =(isset( $_POST [ 'hz-hc-arg1' ]))?trim( $_POST [ 'hz-hc-arg1' ]):null; $comment_author_url =(isset( $_POST [ 'hz-hc-arg2' ]))?trim( $_POST [ 'hz-hc-arg2' ]):null; $comment_content =(isset( $_POST [ 'hz-hc-arg3' ]))?trim( $_POST [ 'hz-hc-arg3' ]):null; |
hz-hc-argX是我定义的参数,大家可以自定义,只要保证这里接受的参数和comment 表单里提交的参数一直就可以了。
接下来修改主题,我用的是mystique,别的主题应该也大同小异。
5.修改wp-content\themes\mystique\functions.php去除主题的更新提示,免得修改过的主题被更新覆盖。增加如下代码:
1 2 3 4 5 6 7 8 9 10 | function themeUpdateHook( $r , $url ) { return $r ; $themes = unserialize( $r [ 'body' ][ 'themes' ] ); unset( $themes [ get_option( 'template' ) ] ); unset( $themes [ get_option( 'stylesheet' ) ] ); $r [ 'body' ][ 'themes' ] = serialize( $themes ); return $r ; } add_filter( 'http_request_args' , 'themeUpdateHook' , 5, 2 ); |
6.修改wp-content\themes\mystique\functions.php,去除WordPress版本。增加如下代码:
1 2 3 4 | function wp_remove_version() { return '' ; } add_filter( 'the_generator' , 'wp_remove_version' ); |
7.修改wp-content\themes\mystique\header.php,去掉如下两行:
1 2 | <meta name= "designer" content= "digitalnature.ro" /> |
如果需要用Google Analytics,增加那段javascript在head段就行了。
8.接下来整合css,mystique会在style.css的前提下增加一个http://xxx/?mystique=css(xxx是你访问的博文地址),用来设定风格,宽度,颜色等后台配置的主要是在这里定义的,但这发起了一次额外的HTTP请求,而且?mystique=css里面有import了另一个css,相当于有三次css请求,而且后面两个css都很小,这样HTTP的开销就显得比较大了。
优化的方法很简单,访问?mystique=css这个地址,看到有import就继续展开,然后去掉import塞到style.css中去就可以了,接着还可以把style.css压缩优化一下。
css优化好之后修改wp-content\themes\mystique\lib\settings.php文件,去掉如下代码:
1 | @import "<?php echo esc_url_raw(add_query_arg('mystique', 'css', (is_404() ? get_bloginfo('url') : mystique_curPageURL()))); ?>" ; |
9.整合js,mystique会发起两个js请求,一个是jquery.mystique.js,另一个是http://xxx/?mystique=jquery_init&ver=2.4.2,用来初始化一下效果。这两个也可以并在一起,将?mystique=jquery_init&ver=2.4.2的内容贴在jquery.mystique.js的最前面就可以了。优化好之后修改wp-content\themes\mystique\lib\settings.php文件,去掉如下代码:
1 | wp_enqueue_script( 'mystique-init' , esc_url_raw(add_query_arg( 'mystique' , 'jquery_init' , (is_404() ? get_bloginfo( 'url' ) : mystique_curPageURL()))), array ( 'jquery' , 'mystique' ), $ver =THEME_VERSION, $in_footer =true); |
10.修改wp-content\themes\mystique\comments.php防止垃圾评论,要同4配合修改,切记参数名一定要一致,而且id和name最好都不要包含author,email等原始的信息。
1 2 3 4 | <input type= "text" name= "hz-hc-arg0" id= "arg0" ... /> <input type= "text" name= "hz-hc-arg1" id= "arg1" ... /> <input type= "text" name= "hz-hc-arg2" id= "arg2" ... /> <textarea name= "hz-hc-arg3" id= "arg3" ... /> |
最后再修改一下wp-content\themes\mystique\js\jquery.mystique.js,把所有对#comment的引用都替换成#你自己定义的id,否则一些ajax效果会丢失。
2010-09-28 00:15
有啊,我访问了一下就有,http://toyst.alwaysdata.net/blog/wp-content/themes/mystique/js/jquery.mystique.js?ver=2.4.2,请用firebug之类的抓包仔细看。。。
2010-09-27 14:28
请问你这个主题的右边那个jquery.mystique.js怎么启用的啊,我也是用Mystique这个主题,但没你那个!