在 function.php 文件中加入以下代碼即可
add_action(‘preprocess_comment’,’yundanran_preprocess_comment’);
function yundanran_preprocess_comment($commentdata){
$comment_post_ID=$commentdata[“comment_post_ID”];
$comment_content=$commentdata[“comment_content”];
$comment_author=$commentdata[“comment_author”];
$comment_author_email=$commentdata[“comment_author_email”];
$comment_author_url=$commentdata[“comment_author_url”];
$comment_author_IP=$commentdata[“comment_author_IP”];
$comment_agent=$commentdata[“comment_agent”];
// 驗證合法
$nonce=wp_create_nonce($comment_post_ID);
if(!isset($_POST[“comment_nonce”]) || $_POST[“comment_nonce”]!==$nonce){
wp_die(“ 請勿惡意評論!”);
}
// 必須輸入中文
if(!preg_match(“/[x{4e00}-x{9fa5}]+/u”,$comment_content))
{
wp_die(“ 請説中國話!”);
}
// 是否在黑名單
if( wp_blacklist_check($comment_author,$comment_author_email,$comment_author_url, $comment_content, $comment_author_IP, $comment_agent )){
wp_die(“ 您已被禁止評論!”);
}
// 是否登錄
if(!is_user_logged_in()){
wp_die(“ 您必須登錄之後才能評論!”);
}
// 過濾 HTML 標籤
$comment_content=strip_tags($comment_content);
return $commentdata;
}