2025-03-24

很多讀者在你的WordPress部落格留言都是緊緊為瞭一個外鏈,所以他們很忙,隨便發個“好文章”“頂一個”這樣毫無意義的迴響,雖然你可以手動刪除他們,但是如果你的網站很受歡迎,刪除迴響也是很耗時間的!

建議限制你的WordPress站點迴響內容的最小字數,這樣應該是可以避免不少簡短的迴響。將下面的代碼添加到當前WordPress主題的 functions.php 文件:

1
2
3
4
5
6
7
8
9
add_filter( 'preprocess_comment', 'minimal_comment_length' );
function minimal_comment_length( $commentdata ) {
	$minimalCommentLength = 20;
	if ( strlen( trim( $commentdata['comment_content'] ) ) < $minimalCommentLength )
        {
		wp_die( '抱歉,您的迴響太短瞭,請至少輸入 ' . $minimalCommentLength . ' 個字!' );
        }
	return $commentdata;
}

add_filter( ‘preprocess_comment’, ‘minimal_comment_length’ );
function minimal_comment_length( $commentdata ) {
$minimalCommentLength = 20;
if ( strlen( trim( $commentdata[‘comment_content’] ) ) < $minimalCommentLength )
{
wp_die( ‘抱歉,您的迴響太短瞭,請至少輸入 ‘ . $minimalCommentLength . ‘ 個字!’ );
}
return $commentdata;
}

註:請根據自己的需求修改第三行的數字。

方法解析:迴響內容保存到數據庫前,使用 preprocess_comment 過濾器來檢查迴響內容。通過 strlen() 函數計算迴響內容的字數,如果小於限制的字數,就通過 wp_die() 函數訪問提示信息!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *