一、评论显示时替换
此方法不会更改评论的原始内容,只会在评论显示给访客时替换相应的关键字,你在后台看到的仍然是评论的原文。在主题目录下的functions.php中将最后一个 ?> 替换成:
/**
* 名称:WordPress评论文字自动替换
* 作者:露兜
* 最后修改:2011年2月20日
*/
function dali_conents_replace($incoming_comment) {
$words = '这里填替换规则';
$rules = explode('||', $words);
foreach($rules as $rule) {
$word = explode('->', trim($rule));
if(isset($word[1]))
$incoming_comment = str_replace(trim($word[0]), trim($word[1]), $incoming_comment);
}
return $incoming_comment;
}
add_filter( 'comment_text', 'dali_conents_replace' );
add_filter( 'comment_text_rss', 'dali_conents_replace' );
?>
请将以上代码中第2行中这里填替换规则替换成你自己的规则,规则请按以下格式填写:
1
关键字A->替换A || 关键字B->替换B || 关键字C->替换C
关键字A在实际显示时将被替换成替换A,依此类推,多个替换规则之间请用 || 隔开。示例:
1
$words = ‘傻逼->牛逼 || shit->haha’;
二、评论添加时替换
此方法将直接替换访客发布的评论内容,数据库中存储的评论就是替换后的内容,在主题目录下的functions.php中将最后一个 ?> 替换成:
/**
* 名称:WordPress评论文字自动替换
* 作者:露兜
* 最后修改:2011年2月20日
*/
function dali_conents_replace($incoming_comment) {
$words = '这里填替换规则';
$rules = explode('||', $words);
foreach($rules as $rule) {
$word = explode('->', trim($rule));
if(isset($word[1]))
$incoming_comment['comment_content'] = str_replace(trim($word[0]), trim($word[1]), $incoming_comment['comment_content']);
}
return $incoming_comment;
}
add_filter( 'preprocess_comment', 'dali_conents_replace' );
?>
标签:
wordpress