All in One SEO中文描述Descriptions支援問題解決方法

wordpress的外掛All in one SEO不知道在幾版後突然就對繁體中文的支援度降低了(消失?)了,在options裡設定自動取得meta descriptions對於中文文章並不會有作用,而且英文字詞間是以空白做為分格與中文字連續沒有分格不同,像是”Hello World !”與”哈囉世界!”,在取descriptions就會有明顯有問題。

搜尋了一下資料找到了一個解決方式,如下:

1.先到外掛編輯器

1.jpg

2.選擇要編輯的外掛All in One SEO Pack,然後按下選取,選擇All-in-one-seo-pack/aioseop.class.php這個檔案。

2.jpg

3.在編輯畫面前端找到$maximum_description_length=150這個變數,把值改成300或更高,建議不要超過450個字,這個變數主要在控制meta descriptions取字的長度,中文字的編碼utf8一個長度是3(?)。

     var $version = "1.6.13.2";
     
     /** Max numbers of chars in auto-generated description */
     var $maximum_description_length = 300;
     
     /** Minimum number of chars an excerpt should be so that it can be used
      * as description. Touch only if you know what you're doing
      */
     var $minimum_description_length = 1;

4.在取字的function裡增加中文字的判斷,在$max = $this->maximum_description_length後面增加一個判斷條件。

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));
    }

如以下

function trim_excerpt_without_filters($text) {
    $text = str_replace(']]>', ']]&gt;', $text);
               $text = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s', '', $text );
    $text = strip_tags($text);
    $max = $this->maximum_description_length;

    if (ord($text[$max])>0x7f&&ord($text[$max])<0xc0){
        while(ord( $text[$max])<=0xc0)
        $max --;
    }
    $text = substr($text, 0, $max);
    return trim(stripcslashes($text));
}

5.字數的判斷,把下方if內的判斷式>的部份變成>=

if (isset($description) && (strlen($description) > $this->minimum_description_length) && !(is_home() && is_paged())) {
                $description = trim(strip_tags($description));
                $description = str_replace('"', '', $description);
            
                // replace newlines on mac / windows?
                $description = str_replace("\r\n", ' ', $description);  

如下:

if (isset($description) && (strlen($description) >= $this->minimum_description_length) && !(is_home() && is_paged())) {
                $description = trim(strip_tags($description));
                $description = str_replace('"', '', $description);
            
                // replace newlines on mac / windows?
                $description = str_replace("\r\n", ' ', $description);  

原本沒有的meta descriptions就出現了

wordpress descriptions

發表迴響