Wordpress代码禁止搜索敏感词汇,需要将以下代码丢进Function.php

Function代码

//禁止搜索敏感词汇,by minirizhi.com
add_action('admin_init', 'minirizhi_search_ban_key');
function minirizhi_search_ban_key() {
    add_settings_field('minirizhi_search_key', 'minirizhi搜索关键词屏蔽', 'minirizhi_search_key_callback_function', 'reading');
    register_setting('reading','minirizhi_search_key');
}
function minirizhi_search_key_callback_function() {
    echo '<textarea name="minirizhi_search_key" rows="10" cols="50" id="minirizhi_search_key" class="large-text code">' . get_option('minirizhi_search_key') . '</textarea>';
}
add_action('template_redirect', 'minirizhi_search_ban');
function minirizhi_search_ban(){
    if (is_search()) {
        global $wp_query;
            $minirizhi_search_key = get_option('minirizhi_search_key');
        if($minirizhi_search_key){
            $minirizhi_search_key = str_replace("\r\n", "|", $minirizhi_search_key);
            $BanKey = explode('|', $minirizhi_search_key);
            $S_Key = $wp_query->query_vars;
    foreach($BanKey as $Key){
        if( stristr($S_Key['s'],$Key) != false ){
        wp_die('<p align="center">请不要搜索非法关键词! <a href="javascript:history.back(-1)">点击返回</a></p>');
                }
            }
        }
    }
}

然后到后台阅读里面设置禁止的敏感词汇,一行一个关键词。保存后搜索试试是不是关键词已经不能搜索了?

文章目录