All In One SEO是我首选的WordPress SEO插件,我一直对它信任有加。直到昨天,用google site了一下自己的网站,发现很多文章的描述都是菜单名称、发表时间之类的,再打开这些文章检查head,发现根本没有meta description描述,我明明选上了All In One SEO自动生成描述那一项,怎么会没有。
怀疑了可以怀疑的所有地方,博客没问题,没有被黒,关了所有的插件换了默认主题,仍然不行。最奇怪的地方是,有些文章可以自动生成描述,有些不行。读了一下代码,锁定问题出在这个函数:
trim_excerpt_without_filters()
问题 – 对中文支持的不好
函数的代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
function trim_excerpt_without_filters( $text ) {
$text = str_replace ( ']]>' , ']]>' , $text );
$text = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s' , '' , $text );
$text = strip_tags ( $text );
$max = $this ->maximum_description_length;
if ( $max < strlen ( $text )) {
while ( $text [ $max ] != ' ' && $max > $this ->minimum_description_length) {
$max --;
}
}
$text = substr ( $text , 0, $max );
return trim( stripcslashes ( $text ));
} |
问题就出在截取字符串的那段代码上,如下
1
2
3
4
5
|
if ( $max < strlen ( $text )) {
while ( $text [ $max ] != ' ' && $max > $this ->minimum_description_length) {
$max --;
}
} |
经过一番搜索,我终于明白这是All In One SEO不支持中文的表现,这段代码只考虑了英文,英文中单词与单词之间靠空格分隔,为了把一个单词截断,所以要找到空格作为分隔。然而中文中很难出现空格,于是就会出现描述很短甚至根本无法输出描述的情况。要解决这个问题只能修改代码,将上面的函数替换成下面这样
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
function trim_excerpt_without_filters( $text ) {
$text = str_replace ( ']]>' , ']]>' , $text );
$text = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s' , '' , $text );
$text = strip_tags ( $text );
$max = $this ->maximum_description_length;
if ( $max < strlen ( $text )) {
while ((ord( $text [ $max ]) & 0x80) != 0 && (ord( $text [ $max ]) & 0x40) == 0 &&
$max > $this ->minimum_description_length) {
$max --;
}
}
$text = substr ( $text , 0, $max );
return trim( stripcslashes ( $text ));
} |
这个问题只出现在自动生成描述时,当你手动填写描述时,All In One SEO会调用另一个函数处理字符串
All In One SEO Meta Description选取优先级
由高到低依次是
- 用户在编辑文章时填写到All In One SEO Description box中的内容,不过滤长度;
- 用户在编辑文章时填写到“摘要”中的内容,使用trim_excerpt_without_filters_full_length()函数过滤,不过滤长度;
- 通过截取文章内容作为描述,通过trim_excerpt_without_filters()函数过滤,如果内容过长就会被截取。
function trim_excerpt_without_filters_full_length($text) {
$text = str_replace(']]>', ']]>', $text);
$text = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s', '', $text );
$text = strip_tags($text);
return trim(stripcslashes($text));
}
总结
为了避免自动描述带来的问题,也为了不让自己辛苦填写的描述再插件被禁用后全部完蛋。
- 将meta description内容填写到摘要里,而不是All In One SEO的Description中,这样即使禁用了All In One SEO,也可以自己写代码调用摘要作为描述,而All In One SEO也会自动将摘要作为meta description。
- 使用自动生成描述的朋友需要手动更新一下trim_excerpt_without_filters()函数。
- 以后在遇到插件不好使的问题,可以直接搜索【插件名称 不支持中文】
转载请注明:有客帮 » 用All In One SEO的童鞋注意了