WordPress纯代码添加搜索验证码功能

由于网站搜索会消耗服务器资源,有网友就想到给站点添加搜索验证功能,减少资源消耗。本次分享吧特意整理了WordPress纯代码添加搜索验证码功能的方法教程,一起来学习使用吧。
WordPress纯代码添加搜索验证码功能-分享吧-https://www.fx8y.com

1、打开你的wordpress主题文件夹

2、找到function文件,添加如下代码即可:

/** 
* WordPress 添加搜索人机验证
* https://www.fx8y.com 
*/
//搜索人机验证
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,'/');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>人机验证</title>
<style>
body {
background-color: #f5f5f5;
color: #333;
font-size: 16px;
font-family: Arial, sans-serif;
text-align: center;
line-height: 1.6;
}
.container {
margin: 50px auto 15px;
max-width: 400px;
padding: 40px 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
background-color: #fff;
}
h1 {
font-size: 24px;
font-weight: normal;
margin-top: 0;
}
form {
margin: 0;
}
.input-group {
display: flex;
align-items: center;
margin-bottom: 20px;
}
.label {
flex: 0 0 80px;
text-align: right;
margin-right: 20px;
}
.input {
flex: 1;
height: 40px;
border: none;
border-bottom: 1px solid #ccc;
padding: 0 10px;
font-size: 16px;
box-sizing: border-box;
}
.btn {
display: block;
width: 100%;
height: 40px;
line-height: 40px;
background-color: #ff5f33;
color: #fff;
font-size: 16px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.btn:hover {
background-color: #ff7854;
}
.btn:focus {
outline: none;
}
a {
color: #666;
font-size: 14px;
text-decoration: none;
}
a:hover {
color: #333;
}
</style>
</head>
<body>
<div class="container">
<h1>人机验证</h1>
<form action="" method="post">
<div class="input-group">
<label class="label"><?php echo $num1;?> + <?php echo $num2;?> =</label>
<input type="text" name="result" class="input" required />
</div>
<button type="submit" class="btn">验证</button>
</form>
</div>
<a href="<?php echo home_url();?>" class="back-link">返回首页</a>
</body>
</html>
<?php
exit;
}
}
}
}
add_action( 'parse_query', 'esc_search_captcha' );

快来试试WordPress纯代码添加搜索验证码功能吧,非常实用的功能哟!

原文链接https://www.fx8y.com/jiaocheng/507.html,转载请注明出处。
 免责声明:根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
 用户须知:分享吧(www.fx8y.com)是非经营性个人站点,所有软件信息均来自网络及网友投稿,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途,网站会员捐赠是您喜欢本站而产生的赞助支持行为,仅为维持服务器的开支与维护,全凭自愿无任何强求。

0
分享海报
显示验证码
没有账号? 注册  忘记密码?