怎样自动替换掉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

转载于:https://www.cnblogs.com/wpsnippet/archive/2012/12/20/2826704.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值