level 14
第一步:WordPress数据库优化
大家都知道WordPress是php+mysql环境下的建站程序,那么在大数据的情况下程序运行的瓶颈就是数据查询。那么为了减小数据库的大小,最有效的方法就是删除冗余数据以及禁止WordPress自动保存文章草稿、文章修订等无用数据。仅需在当前WordPress主题的functions.php文件中加上以下代码即可禁用WordPress的自动保存功能。
//禁止wptexturize函数
remove_filter('the_content', 'wptexturize');
remove_action('pre_post_update', 'wp_save_post_revision' );
add_action( 'wp_print_scripts', 'disable_autosave' );
function disable_autosave() { wp_deregister_script('autosave');}
禁用了自动保存和修订版本后,WordPress还是会产生一定的冗余数据。创建一个新php文件,粘贴以下代码执行:
<?php
//wordpress数据库优化脚本 by v7v3.com
$hostname_blog = "localhost";//设定数据库主机,同wp-config.php
$database_blog = "wordpress";//设定数据库名,同wp-config.php
$username_blog = "root";//设定数据库用户名,同wp-config.php
$password_blog = "";//设定数据库密码,同wp-config.php
$blog = mysql_pconnect($hostname_blog, $username_blog, $password_blog) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_blog, $blog);
mysql_query('DELETE FROM wp_posts WHERE post_type = "revision"');
mysql_query('DELETE FROM wp_postmeta WHERE meta_key = "_edit_lock"');
mysql_query('DELETE FROM wp_postmeta WHERE meta_key = "_edit_last"');
mysql_query('DELETE FROM wp_commentmeta WHERE meta_key LIKE "%trash%"');
mysql_query('DELETE FROM wp_comments WHERE comment_approved = "trash"');
mysql_query('DELETE FROM wp_options WHERE option_name REGEXP "_transient_"');
mysql_query('DELETE FROM wp_postmeta WHERE meta_key = ‘_wp_attached_file’');
mysql_query('DELETE FROM wp_postmeta WHERE meta_key = ‘_wp_attachment_metadata’');
mysql_query("delete from wp_posts
where (post_status='auto-draft' or post_status='inherit')
and post_type='post'");
$tablelist = mysql_query("SHOW TABLES");
while($checklist = mysql_fetch_array($tablelist)) {
$optimization=mysql_query("OPTIMIZE TABLE `$checklist[0]`");
}echo 'Done';
//ps记得修改数据库前缀~
使用时将脚本上传至网站任意目录后并且通过浏览器访问即可一键优化wordprsss数据库。 然后再屏蔽垃圾评论提交到数据库,这样数据库就优化的差不多了,代码依然来自v7v3:
//禁止垃圾评论提交到数据库
function v7v3_fuckspam($comment) {
if( is_user_logged_in()){ return $comment;} //登录用户无压力...
if( wp_blacklist_check($comment['comment_author'],$comment['comment_author_email'],$comment['comment_author_url'], $comment['comment_content'], $comment['comment_author_IP'], $comment['comment_agent'] )){
header("Content-type: text/html; charset=utf-8");
wp_die('
您评论包含辱骂,过激或者违反法律等言论,或者您的IP已被加入黑名单,如有疑问请联系管理员处理!<a href="javascript:history.go(-1);">返回文章页</a>
');
} else {
return $comment;
}
}
add_filter('preprocess_comment', 'v7v3_fuckspam');
2014年05月18日 00点05分
2
level 14
第三步:更换一个速度快的新主机
即使我们对WordPress做了极致优化,但仍然逃离不过瓶颈的限制——主机。假若你的主机在美国、或是捷克,甚至德国,那么再怎么优化还不如购买一个优质的国内/香港主机。如果你使用的是VPS,甚至还有更多的方法去加速网站:memcache、xcache、全站静态……优化的方法无穷无尽,若您也找到了好的优化方法,请与我们分享。
2014年05月18日 00点05分
4