怎样自动替换掉WordPress评论中的文字
一共有两种方法,都是修改主题的方法。
1:评论显示时替换
此方法不会更改评论的原始内容,只会在评论显示给访客时替换相应的关键字,你在后台看到的仍然是评论的原文。在主题目录下的functions.php中将最后一个 ?>替换成:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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行中这里填替换规则替换成你自己的规则,规则请按以下格式填写:
关键字A->替换A || 关键字B->替换B || 关键字C->替换C |
关键字A在实际显示时将被替换成替换A,依此类推,多个替换规则之间请用 || 隔开。示例:
$words = '傻逼->牛逼 || shit->haha'; |
2:评论添加时替换
此方法将直接替换访客发布的评论内容,数据库中存储的评论就是替换后的内容,在主题目录下的functions.php中将最后一个 ?> 替换成:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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' ); ?> |
替换规则的书写方式请参考以上的方法一。
转载自:
http://www.ludou.org/wordpress-comment-text-replace.html

浙公网安备 33010602011771号