Posts Tagged ‘php’

清理主题文件,加速Wordpress

Wednesday, January 23rd, 2008

Wordpress性能差、速度慢是出了名的,加速Wordpress除了安装一些缓存/静态化插件,例如WP-CacheWP-Super-CacheCos-Html-Cache,还可以通过清理主题文件中的一些数据库查询语句,减少Http请求次数,达到优化的目的。

Wordpress主题文件中经常会用到的查询语句主要有以下几个,这些语句完全可以去除或转化为静态方式而无须访问数据库 :

<html xmlns=”http://www.w3.org/1999/xhtml” <?php language_attributes() ?>> 转化为
<html lang=”zh-CN dir=”ltr xmlns=”http://www.w3.org/1999/xhtml>

<meta http-equiv=”content-type” content=”<?php bloginfo(’html_type’) ?>; charset=<?php bloginfo(’charset’) ?>” /> 转化为
<meta content=”text/html; charset=UTF-8 http-equiv=”content-type/>

<meta name=”description” content=”<?php bloginfo(’description’) ?>” />转化为
<meta name=”description” content=”这里直接输入你的博客简介” />

<meta name=”generator” content=”WordPress <?php bloginfo(’version’) ?>” /> 这句可以删除

<link rel=”stylesheet” type=”text/css” href=”<?php bloginfo(’stylesheet_url’); ?>” /> 直接写出style.css文件的链接地址

<link rel=”alternate” type=”application/rss+xml” href=”<?php bloginfo(’rss2_url’) ?>”  /> 直接写出RSS的链接地址

<link rel=”pingback” href=”<?php bloginfo(’pingback_url’) ?>” /> 直接写出pingback的链接地址

以此类推,凡是可以变为静态方式的地方都直接写出具体内容,这些语句大都存在于header.php和footer.php文件中,通过这番清理,大概可以减少10多次的数据库查询。

想进一步优化Wordpress的朋友还可以参考下面两篇文章介绍的方法 : 《优化Wordpress-wp终极优化手册》、《加快WordPress的页面生成和载入速度》。

注 : 本文参考了Speed up and clean up your WordPress!

多循环时的翻页问题

Monday, July 9th, 2007

另一个Blog使用了自己在Sandbox主题上改的一个模板,index.php中调用了两个循环(loop),语句如下:

while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>

天佑。不使用query_posts()也可以实现我想要的功能,修改后第二个loop的语句如下:

…….
if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>

———other code———


使用in_category()来排除shorts分类中的帖子,但这种方法有个缺陷,实际显示的帖子数是WP后台Option中设定的Post数减去在这个范围内出现的shorts分类帖子的数目,比如后台设定显示10个帖子,而这10个帖子中有两个在shorts分类里,那么实际将显示10-2=8个帖子,有没有更好的方法呢?

更多有关Loop的介绍可以参考Wordpress的官方说明

Dreamhost的回复

Friday, February 9th, 2007
The problem you experienced was due to us upgrading PHP 5 last night from
5.1.2 to 5.2, which also included a new memory restriction. We’re trying
to implement it for better server stability. It looks like your WP
process was trying to use an excessive amount of memory and it errored
out. Do you have an custom plugins on your WP installs? They may need to
be removed in the future.

We realize that upgrades can break things, and we did do some testing
beforehand as well. However, we’re trying to move ahead and provide the
latest resources for our customers, and this version, which came out in
November, includes many security fixes, among others. We need our
customers’ cooperation to make this work. I noticed that you’re using an
older version of WP on that domain, so upgrading that will be necessary
to match the new PHP version. In general, keeping uptodate with the
applications you’re using is always a good idea - security updates, new
features, etc. It looks like you already did that.

We had rolled back the upgrade for now, and we’ll be sending an
announcement before we attempt the upgrade again, most likely within a
week from now. We will be rethinking the actual amount of the memory
limit, but we will be placing one on PHP processes. So, let me know on
your custom add-ons, plugins, so we can figure out a relatively
acceptable limit that will also ensure stable servers.

看了Dreamhost的回复,问题基本清楚了,这次的fatal error的根源还是Dreamhost升级了PHP版本到5.2,并作了新的内存限制,Google Sitemaps插件虽然有内存泄露的问题,但在之前确实工作良好,这次由于DH的内存限制才爆发了隐患。不能说DH升级不对,但他们应该在确保完全无问题的情况下再升级,并且应该事先通知,显然这一点有疏忽,不过我对他们的回复还是比较满意的 :wink: ,我的租用期快到了,如无意外,我想我会继续使用DH的服务 :cool:

升级到Wordpress 2.0.4

Monday, July 31st, 2006

Wordpress已经更新到了2.0.4版,同2.0.3版一样,这个版本仍然是一个安全增强和bug修补版,升级很顺利,目前我所使用的插件都能够正常工作,但对于中文用户,仍需要修改wp-db.php文件,否则会出现乱码。

wp-db.php在wp-includes目录中,编辑该文件,找到

If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

“);
}
$this->select($dbname);
}

在其中加上一条语句$this->query(”SET NAMES ‘utf8′ “);,修改后如下:

If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

“);
}
$this->query(”SET NAMES ‘utf8′ “);
$this->select($dbname);
}