分类目录归档:wordpress

禁止cos_slug_translator将全英文标题url翻译为中文

cos_slug_translator是一个很好用的插件,会自动将带有中文的标题翻译为英文,作为永久链接,英文比中文更好。
cos_slug_translator采用了有道的翻译api,目前版本是1.1,这个api会将中文翻译为英文,同时,也会将英文翻译为中文。没有参数可以指定是 中->英 还是 英->中。这样就造成了如果标题是全英文的,那么最终的固定连接会自动被替换为翻译过来的中文,反倒不美。
进行如下修改,如果表里中不含有中文,就不进行翻译

修改 function CosSlugTrans($postID){
    $tableposts = $wpdb->posts ;
        $sql = "SELECT post_title,post_name FROM $tableposts WHERE ID=$postID";
        $res = $wpdb->get_results($sql);    
        $post_title = $res[0]->post_title;
        $tran_title = cos_tranlate($post_title);
        $slug = $tran_title;
        if(function_exists("sanitize_title") ) {
            if( sanitize_title( $res[0]->post_title ) != $res[0]->post_name  ){
                if( !substr_count($path, '%') ) 
....
修改为
    $tableposts = $wpdb->posts ;
        $sql = "SELECT post_title,post_name FROM $tableposts WHERE ID=$postID";
        $res = $wpdb->get_results($sql);    
        $post_title = $res[0]->post_title;

        if (preg_match("/[\x7f-\xff]/", $post_title)) {
            $tran_title = cos_tranlate($post_title);
            $slug = $tran_title;
        } else {
            $slug = $post_title;
        }

        if(function_exists("sanitize_title") ) {
            if( sanitize_title( $res[0]->post_title ) != $res[0]->post_name  ){
                if( !substr_count($path, '%') ) 
....

 

disable wp-cron.php

trace wp-super-cache的时候,发现后台有很多wp-cron.php的调用,每一次访问页面都会调用。

01:32:22 /wp-cron.php?doing_wp_cron=1341019942.6320919990539550781250 In WP Cache Phase 2
01:32:22 /wp-cron.php?doing_wp_cron=1341019942.6320919990539550781250 Setting up WordPress actions
01:32:22 /wp-cron.php?doing_wp_cron=1341019942.6320919990539550781250 Not caching POST request.
01:32:22 /wp-cron.php?doing_wp_cron=1341019942.6320919990539550781250 Cache garbage collection.
01:32:22 /wp-cron.php?doing_wp_cron=1341019942.6320919990539550781250 Cleaning expired cache files in /****/public_html/wp-content/cache/
01:32:22 /wp-cron.php?doing_wp_cron=1341019942.6320919990539550781250 Doing GC on supercache dir: /****/public_html/wp-content/cache/supercache
01:32:22 /wp-cron.php?doing_wp_cron=1341019942.6320919990539550781250 gc: could not delete /****/public_html/wp-content/cache/supercache/6658.net/2012/01 as it’s not empty: 30

google之,发现该进程会占用很多系统资源。用google到的如下方法disable
在wp-config.php中添加

define(‘DISABLE_WP_CRON’, true);

然后在系统的crontab中添加

0,30 * * * * php -q /path_to_your_website/public_html/wp-cron.php

参考文档
http://blog.mellowhost.com/reducing-cpu-usage-for-wordpress-users.html

nginx+wp-super-cache不生成静态cache文件

网站迁移到nginx后,所有原来apache下面的配置文件都要重新写。
配置完wp-super-cache后,查看后台,发现cache目录已经建立,但是没有生成html文件。
打开wp-super-cache的debug功能,查看后台log

00:23:15 /2012/06/27/yourls-rewrite-apache-to-nginx/ Cookie detected: wordpress_logged_in_08459be021d7ddc91806841039286bce
00:23:15 /2012/06/27/yourls-rewrite-apache-to-nginx/ No wp-cache file exists. Must generate a new one.
00:23:15 /2012/06/27/yourls-rewrite-apache-to-nginx/ Cookie detected: wordpress_logged_in_08459be021d7ddc91806841039286bce
00:23:15 /2012/06/27/yourls-rewrite-apache-to-nginx/ In WP Cache Phase 2
00:23:15 /2012/06/27/yourls-rewrite-apache-to-nginx/ Setting up WordPress actions
00:23:15 /2012/06/27/yourls-rewrite-apache-to-nginx/ Supercache caching disabled. Only using wp-cache. Non empty GET request.
00:23:15 /2012/06/27/yourls-rewrite-apache-to-nginx/ Created output buffer
00:23:15 /2012/06/27/yourls-rewrite-apache-to-nginx/ Output buffer callback
00:23:15 /2012/06/27/yourls-rewrite-apache-to-nginx/ Supercache disabled: GET or feed detected or disabled by config.
00:23:15 /2012/06/27/yourls-rewrite-apache-to-nginx/ Writing non-gzipped buffer to wp-cache cache file.

发现是因为有GET参数传递给了wp-super-cache,所以disabled.
wordpress所有响应都是rewrite给index.php的,检查nginx rewrite规则。发现确实使用用参数传递给index.php的,

try_files $uri/ $uri /index.php?q=$uri&$args;

网上查找一下,换成如下的rewrite方法

if (!-e $request_filename) { rewrite ^/(.+)$ /index.php last; }

reload配置之后,再次访问同一页面,后台trace如下

01:11:49 /2012/06/27/yourls-rewrite-apache-to-nginx/ Sending buffer to browser
01:11:49 /2012/06/27/yourls-rewrite-apache-to-nginx/ wp_cache_shutdown_callback: collecting meta data.
01:11:49 /2012/06/27/yourls-rewrite-apache-to-nginx/ Did not write meta file: wp-cache-6848e4f0747fac2d2b5b769950bff440.meta *1* *0* *1*
01:13:08 /2012/06/27/yourls-rewrite-apache-to-nginx/ supercache dir: /****/public_html/wp-content/cache/supercache/3qs.com/2012/06/27/yourls-rewrite-apache-to-nginx/
01:13:08 /2012/06/27/yourls-rewrite-apache-to-nginx/ No wp-cache file exists. Must generate a new one.
01:13:08 /2012/06/27/yourls-rewrite-apache-to-nginx/ In WP Cache Phase 2
01:13:08 /2012/06/27/yourls-rewrite-apache-to-nginx/ Setting up WordPress actions
01:13:08 /2012/06/27/yourls-rewrite-apache-to-nginx/ Created output buffer
01:13:08 /2012/06/27/yourls-rewrite-apache-to-nginx/ Output buffer callback
01:13:08 /2012/06/27/yourls-rewrite-apache-to-nginx/ Anonymous user detected. Only creating Supercache file.
01:13:08 /2012/06/27/yourls-rewrite-apache-to-nginx/ Writing non-gzipped buffer to supercache file.
01:13:08 /2012/06/27/yourls-rewrite-apache-to-nginx/ Renamed temp supercache file to /****/public_html/wp-content/cache/supercache/3qs.com/2012/06/27/yourls-rewrite-apache-to-nginx/index.html
01:13:08 /2012/06/27/yourls-rewrite-apache-to-nginx/ Sending buffer to browser
01:13:08 /2012/06/27/yourls-rewrite-apache-to-nginx/ wp_cache_shutdown_callback: collecting meta data.

到后台服务器上产看, 已经生成了对应的index.html静态文件了,访问速度也从1500多ms降到500多ms,多次访问,会返回304,在本地也cache了。。

[09:17:41.414] GET http://3qs.com/2012/06/27/yourls-rewrite-apache-to-nginx/ [HTTP/1.1 304 Not Modified 204ms]

-THE END-

更正历史blog的图片链接错误

今天整理blog,发现部分blog的图片缺失,经过查找,原因是多次迁移,造成原来在/wp-content/upload/year/month/下面的图片都被放到了/wp-content/upload/下面,路径发生改变。

用phpmyadmin,如下sql,查找用旧链接位置的帖子
SELECT *
FROM `3qs_wp_posts`
WHERE post_content LIKE ‘%www.3qs.com/file%’

用如下sql更新
UPDATE 3qs_wp_posts SET post_content=REPLACE(post_content, ‘www.3qs.com/files’, ‘3qs.com/wp-content/uploads/2011/03’) where id=458

然后手工将/wp-content/upload下面的图片移动到对应的/wp-content/upload/year/mongth下面。

进行中~~~~~~~~~~

为wordpress复制文章时自动加版权

给wordpress博客内容加版权网上还没有现成的插件,不过个人认为wordpress最好的地方就是源码可以随便修改,从后台主题编辑就行,相信这也是很多网友选择wordpress的原因吧。

给文章加版权分两种,一种是在文章上方或下方显示你的版权声明,这种方法可以加以下代码

    <p>转载原创文章请注明,转载自:<strong><?php bloginfo(‘name’); ?></strong>[<a href=”<?php echo get_settings(‘home’); ?>”><?php echo get_settings(‘home’); ?></a>]</p> <p>本文链接: <a href=”<?php the_permalink() ?>” title=”<?php the_title(); ?>”><?php the_permalink(); ?></a></p>
    那到底应该放到什么地方呢? 找到single.php(单篇文章页),找到类似下面的这样一句话: <?php the_content(); ?>

继续阅读