在WordPress主题中添加敏感词过滤功能的方法有很多,以下是一种常见的实现方式:
function filter_sensitive_words($content) {
// 定义敏感词列表
$sensitive_words = array('敏感词1', '敏感词2', '敏感词3');
// 将敏感词替换为 ***(也可根据需要修改为其他方式)
$filtered_content = str_ireplace($sensitive_words, '***', $content);
return $filtered_content;
}
function filter_content($content) {
if (is_single() || is_page()) {
$content = filter_sensitive_words($content);
}
return $content;
}
add_filter('the_content', 'filter_content');
上述代码使用了WordPress提供的the_content
过滤器,针对文章或页面的内容进行过滤。当显示文章或页面内容时,会通过filter_content
函数进行敏感词过滤。
functions.php
文件中即可。需要注意的是,以上代码仅提供了一个简单的实现示例,具体情况可能因需求而异,可以根据实际情况进行调整和扩展。