本站在之前曾经介绍过提高网站速度的6个方法,今天主要针对WordPress来说说,如何对WordPress进行优化,从而提高网站的访问速度。

1、使用WordPress 的eAccelerator插件

还是先解释一下什么是eAccelerator ,eAccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了性能php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。 eAccelerator还可以对脚本起优化作用,以加快其执行效率,使您的PHP程序代码执效率能提高1-10倍。

现在已经有高手为WordPress开发出了专门的eAccelerator插件。大家只要下载这个插件,然后放入到 wp-content 文件夹下即可,不用做其他设置,注意不是放到 plugin 文件夹。

2、在WordPress后台使用压缩技术

一般有两种方法来实现压缩,一种是在后台开启 GZIP 压缩功能(默认用的是 ob_gzhandler);

还有一种是使用 zlib 压缩,方法为在 htaccess 中加入下面两行:

php_flag zlib.output_compression on
php_value zlib.output_compression_level 5

其中第二种方法要看虚拟主机是否支持。

3、对js 脚本进行压缩

如果 js 文件比较大的话,压缩是非常必要的,比如 prototype.js, jQuery.js 这些大库加起来的话都有 100 多K, 压缩后文件大小可以缩小3、4倍。

在 js 文件的开头加上:
< ?php
// check to see if the user has enabled gzip compression in the WordPress admin panel
if ( ob_get_length() === FALSE and !ini_get(’zlib.output_compression’) and ini_get(’output_handler’) != ‘ob_gzhandler’ and ini_get(’output_handler’) != ‘mb_output_handler’ ) {
ob_start(’ob_gzhandler’);
}

// The headers below tell the browser to cache the file and also tell the browser it is JavaScript.
header(”Cache-Control: public”);
header(”Pragma: cache”);

$offset = 60*60*24*60;
$ExpStr = “Expires: “.gmdate(”D, d M Y H:i:s”,time() + $offset).” GMT”;
$LmStr = “Last-Modified: “.gmdate(”D, d M Y H:i:s”,filemtime(__FILE__)).” GMT”;

header($ExpStr);
header($LmStr);
header(’Content-Type: text/javascript; charset: UTF-8′);
?>

然后另存为 xxxx.js.php, 再你的模板 head 部分,把原来的 js 代码换为下面的样式即可。

< script type="text/javascript" xsrc="javascript/xxxx.js.php">