为了减小服务器数据库的压力还有避免一些对网站搜索功能的恶意应用比如:
我们就需要限制每一次的搜索时间间隔。
具体方法如下:
- 打开wordpress当前启用的主题的functions.php文件,拉倒最后一行。
- 在最后一行添加如下代码后保存:
//搜索间隔设置
add_action('admin_init','searchInterval');
function searchInterval(){
add_settings_field('sI','搜索间隔控制','sIAdd','reading');
register_setting('reading','sI');
}
function sIAdd(){
echo '<textarea name="sI" rows="1" cols="1" id="sI" class="large-text code">' . get_option('sI') . '</textarea>
<br><more>输入秒即可如想要设置10秒在空格中输入"10"</more>';
}
add_action('template_redirect', 'searchIntervalRealize');
function searchIntervalRealize(){
$currentTime = time();
$sI = get_option('sI');
$IntervalTime = str_replace("rn", "|", $sI);
if(empty($IntervalTime)||$IntervalTime < 0){
wp_die('请在后台设置正确的间隔时间');
}
if (is_search()){
if( empty($_COOKIE['6r6_searchInterval']) ){
setcookie('6r6_searchInterval',$currentTime,$currentTime+$IntervalTime);
}else{
$beforeTime = $_COOKIE['6r6_searchInterval'];
$DValue = $currentTime - $beforeTime;
if ( $DValue < $IntervalTime ){
$errorInfo = '请在'.$IntervalTime .'秒后再次使用搜索功能';
wp_die($errorInfo);
}
}
}
}
- 保存完毕后会在后台的settings->阅读项目中看到如下选项。
- 在输入框中输入需要限制的秒数即可,不要输入一些花里胡哨的东西,我并没有做过多的限制,可能会导致网站无法使用!