WordPress内置的搜索是一个很占内存的功能,如果你的文章很多,那么执行一次搜索会相对卡顿,那么我们如何尽可能的防范一下呢?比如机器人扫描到了搜索页面,那将可能直接导致内存爆满mysql进程被终止。
为 WordPress 搜索添加人机验证
我们可以加一个简单的搜索验证机制,用户在第一次搜索时需要进行简单的人机验证。一来这样可以有效防止恶意扫描导致内存崩溃,二来可以防止恶意请求关键字生成结果页面。
可将下面代码加到主题的functions.php里即可。
function esc_search_captcha( $query, $error = true ) {
if ( is_search() && !is_admin() ) {
if ( ! isset( $_COOKIE['esc_search_captcha'] ) ) {
$query->is_search = false;
$query->query_vars['s'] = false;
$query->query['s'] = false;
if ( $error == true ){
//$query->is_404 = true;
if ( isset( $_POST['result'] ) ) {
if ( $_POST['result'] == $_COOKIE['result'] ) {
$_COOKIE['esc_search_captcha'] = 1;
setcookie('esc_search_captcha',1,0,'/');
echo '<script>location.reload();</script>';
}
}
$num1 = rand(1,50);
$num2 = rand(1,50);
$result = $num1+$num2;
$_COOKIE['result'] = $result;
setcookie('result',urldecode($result),0,'/');
?>
<html>
<head>
<meta charset="UTF-8">
<title>搜索验证 | 怸歪小站</title>
<link rel='stylesheet' id='caomei-css' href='https://cycy.fun/wp-content/themes/Wing-main/static/caomei/style.css?ver=0.8.10' type='text/css' media='all' />
<style>
body.custom-background, body{background: url(https://cycy.fun/img/bj2.png) no-repeat fixed center / cover !important;}
.be-search-captcha-box{position:fixed;top:200px;left:50%;margin:0 0 0 -130px;z-index:9}
.be-search-captcha{position:relative;width:260px;color:var(--be-grey-3);padding:20px 20px 15px 20px;text-align:center;border-radius:8px;background:var(--be-bg-glass);-webkit-backdrop-filter:saturate(5) blur(20px);backdrop-filter:saturate(5) blur(20px);transform:translateZ(0);box-shadow:0 4px 16px 0 rgb(0 0 0 / 40%);-webkit-box-shadow:0 4px 16px 0 rgb(0 0 0 / 40%)}
.be-search-captcha a{color:var(--be-grey-3)}
.be-search-captcha-tip{font-size:1.2rem;color:var(--be-grey-3);margin:0 0 15px 0}
.be-search-captcha input{width:50px;text-align:center;font-size:16px;border-radius:3px;border:none;-webkit-appearance:none;box-shadow:0 0 0 1px var(--be-shadow)}
.be-search-captcha input:focus{outline:none}
.be-search-captcha button{background:transparent;color:var(--be-body);outline:none;cursor:pointer;margin:0 0 0 10px;padding:2px 10px;border-radius:3px;border:none;box-shadow:0 0 0 1px var(--be-shadow)}
.be-search-captcha button:hover{color:var(--be-white);background:var(--be-bg-blue)}
.be-search-captcha-btu{background:transparent;display:inline-block;margin:10px 0 0 0;padding:5px 10px}
@media screen and (max-width:768px){.be-search-captcha-box{top:100px}
.wp-block-search__button,.searchbar button{width:20%}
.wp-block-search__input,.search-input input{width:80%}
#search-main .search-cat{right:20%}
}
#search-main .search-cat .nice-select{background:transparent;height:35px;line-height:33px;border-radius:0;border:none}
.search-nav{float:left;width:100%}
.search-menu a{background:var(--be-bg-pink-fd);float:left;color:var(--be-grey-5);line-height:30px;margin:0 5px 5px 0;padding:2px 10px;border-radius:5px;-webkit-transition-duration:0.3s;transition-duration:0.3s}
.search-menu a:hover{background:var(--be-shadow-h);color:var(--be-white)}
.choose{display:none}
.soico{width:1.4em !important;height:1.4em !important}
@media screen and (max-width:1025px){.ajax-content-box{display:none !important}
.not-found .search-cat,.single-content .search-cat,.search-page .search-cat{position:inherit;width:100%;text-align:center;margin:10px 0 0 0;display:block;border:none}
.search-wrap{width:98%}
}.sod_select,.sod_select *{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none}
.sod_select{background:#ebebeb;position:relative;display:inline-block;padding:11px;color:var(--be-grey-3);font-size:13px;text-align:left;line-height:1;outline-offset:-2px;cursor:pointer}
.sod_select:before,.sod_select:after{position:absolute;font-family:be;right:10px;font-size:13px}
.sod_select:after{content:"\e76f";font-family:be;top:auto;bottom:12px}
.sod_select .sod_label{padding-right:15px;line-height:15px}
.sod_select .sod_label:hover{color:var(--be-grey-9)}
.sod_select .sod_list{position:absolute;top:100%;left:0;display:none;height:219px;width:150px;margin:0 0 0 -1px;background:var(--be-bg-white);border-top:none;z-index:1}
.post .sod_select .sod_list{margin:0}
.sod_select.open .sod_list{display:block;border:1px solid var(--be-border-z)}
.sod_select .sod_list ul{overflow-y:auto;padding:0;margin:0}
.sod_select .sod_list li{height:43px;line-height:43px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;position:relative;padding:0 10px !important;margin:0 !important;border-bottom:none !important}
.sod_select .sod_list .active{background:#ebebeb}
.sod_select .sod_list .selected{font-weight:700;padding-right:25px}
.sod_select.touch select{-webkit-appearance:menulist-button;position:absolute;top:0;left:0;display:block !important;height:100%;width:100%;opacity:0;z-index:1}
.sod_select select{display:none !important}
:root{--be-shadow-plus:0 0 0 1px rgba(0,0,0,0.073) inset,0 0 6px 3px rgba(0,0,0,0.073);--be-shadow-nav:0 0 0 1px rgba(0,0,0,0.073) inset,0 2px 3px 1px rgba(0,0,0,0.073);--be-bg-gradual:linear-gradient(to bottom,transparent 0%,#fff 50%);--be-bg-glass-m:rgba(255,255,255,0.8);--be-bg-glass:rgba(255,255,255,0.8);--be-shadow:rgba(0,0,0,0.073) inset;--be-bg-body:#eff0f2;--be-bg-white-l:#fff;--be-bg-white-b:#fff;--be-bg-white-s:#fff;--be-bg-cyan:#98d2cc;--be-bg-tou:#f6f7f7;--be-bg-grey-3:#333;--be-grey-nav-3:#333;--be-bg-grey-5:#555;--be-bg-grey-6:#666;--be-bg-grey-9:#999;--be-bg-grey-a:#999;--be-bg-grey-f8:#f7f8f9;--be-bg-grey-tab:#f7f8f9;--be-bg-grey-on:#eee;--be-bg-grey-e:#f7f8f9;--be-bg-grey-h:#f7f8f9;--be-bg-grey-e8:#e8e8e8;--be-bg-grey-top:#eff0f2;--be-bg-blue-d:#ddd;--be-bg-blue:#4d8cb8;--be-bg-eye:#f6f1e7;--be-bg-reading:#e8c2ad;--be-bg-reading:#e8c2ad;--be-bg-m:#4d8cb8;--be-m-bg:#4d8cb8;--be-bg-m-text:#4d8cb8;--be-bg-btn:#4d8cb8;--be-bg-btn-s:#4d8cb8;--be-bg-hd:#4d8cb8;--be-bg-ht:#4d8cb8;--be-bg-htwo:#c40000;--be-bg-yellow:#fa9f7e;--be-bg-red:#c40000;--be-bg-ico:#c40000;--be-bg-red-f4:#ff4400;--be-bg-pre:#2a3139;--be-bg-cf:#fffcf7;--be-yellow:#fa9f7e;--be-blue:#4d8cb8;--be-white:#fff;--be-white-a:#fff;--be-red:#c40000;--be-bg-cat:#c40000;--be-purple:#bb9998;--be-grey-3:#333;--be-grey-top:#333;--be-blue-top:#4d8cb8;--be-grey-4:#444;--be-grey-5:#555;--be-grey-6:#666;--be-grey-7:#777;--be-search:#777;--be-grey-8:#888;--be-grey-9:#999;--be-grey-c:#ccc;--be-bread:#666;--be-red-f4:#ff4400;--be-bg-pink-fd:#fdf8f3;--be-arrow-w:#ebebeb;--be-arrow-n:#fff;--be-white-fw:#fff;--be-grey-coy:#f2f2f2;--be-grey-co:#f7f8f9;--be-grey-coh:#f7f8f9;--be-nav-width:1300px;--be-nav-98:98%;--be-main-width:1200px;--be-main-percent:98%;--be-m-li:81px;--be-h:81px;--be-h-s:111px;--be-admin:#333;--be-admin-btn:#444;--be-admin-5:#000;--be-admin-name:#444;--be-bg-cover:linear-gradient(to right,rgba(23,9,49,.6) 0%,rgba(30,95,116,.6) 100%);--be-wave-0:rgba(255,255,255,0.7);--be-wave-3:rgba(255,255,255,0.5);--be-wave-5:rgba(255,255,255,0.3);--be-wave-7:rgba(255,255,255,1)}
</style>
</head>
<body>
<div class="be-search-captcha-box">
<div class="be-search-captcha czs-search-l">
<div class="be-search-captcha-tip">输入答案查看搜索结果</div>
<form action="" method="post"><?php echo $num1;?> + <?php echo $num2;?> = <input type="text" name="result" required /> <button type="submit">确定</button></form>
<a href="<?php echo home_url();?>">返回首页</a>
</div></div></div>
</body>
</html>
<?php
exit;
}
}
}
}
add_action( 'parse_query', 'esc_search_captcha' );
如果没有使用第三方的搜索,为WP默认搜索加个验证,还是非常必要的,效果可以看本站搜索。验证过一次后,只有关闭浏览器或者清空浏览器 cookie 才会需要再次验证。
本文章转自知更鸟:https://zmingcx.com/search-captcha-for-wordpress.html,样式借用了知更鸟的样式。更多技术文章请访问知更鸟。
声明:怸歪小站 | 本文采用署名-非商业性使用-相同方式共享 4.0 国际许可协议[CC BY-NC-SA]进行授权
文章名称:【转载】为 WordPress 搜索添加人机验证(借用知更鸟样式)
文章链接:https://cycy.fun/2025/02/sousuoyanzheng/
本站文章若无特别说明,皆为原创,转载请注明来源:怸歪小站,如果您喜欢可以订阅本站RSS feed
文章名称:【转载】为 WordPress 搜索添加人机验证(借用知更鸟样式)
文章链接:https://cycy.fun/2025/02/sousuoyanzheng/
本站文章若无特别说明,皆为原创,转载请注明来源:怸歪小站,如果您喜欢可以订阅本站RSS feed
转载声明