这段代码的作用是在WordPress的搜索功能中添加人机验证,在用户进行搜索时,如果没有通过人机验证,则不允许进行搜索,需要重新输入正确的结果才能进行搜索。具体实现是通过拦截WordPress的查询操作,判断是否为搜索操作,如果是则进行人机验证。如果验证通过,则设置一个cookie,以后再进行搜索时就不需要再进行验证了。如果验证未通过,则输出人机验证的界面,要求用户输入正确的计算结果才能进行搜索。
第一种方式
将下面代码添加到主题函数模板 functions.php中:
/** * WordPress 纯代码添加搜索人机验证 * https://www.ypojie.com/12140.html */ //搜索人机验证 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' );
第二种方式
将下面代码添加到主题函数模板 functions.php中:
/** * WordPress 纯代码添加搜索人机验证 * https://www.ypojie.com/12140.html */ //搜索人机验证 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> <style> body { font-family: Arial, sans-serif; background-color: #f5f5f5; } .erphp-search-captcha { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); max-width: 350px; width: 100%; padding: 30px; border-radius: 5px; background-color: #f8f8f8; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); font-size: 18px; line-height: 1.5; text-align: center; } .erphp-search-captcha form { margin-top: 20px; } .erphp-search-captcha input { display: inline-block; width: 60px; height: 40px; margin-right: 10px; font-size: 20px; text-align: center; border: 2px solid #ddd; border-radius: 3px; transition: border-color 0.2s; } .erphp-search-captcha input:focus { outline: none; border-color: #ff5f33; } .erphp-search-captcha button { display: inline-block; padding: 0 20px; height: 40px; line-height: 40px; font-size: 18px; font-weight: bold; color: #fff; background-color: #ff5f33; border: none; border-radius: 3px; cursor: pointer; transition: background-color 0.2s; } .erphp-search-captcha button:hover { background-color: #d44c2e; } .erphp-search-captcha button:focus { outline: none; } .erphp-search-captcha a { display: block; margin-top: 20px; color: #666; font-size: 14px; text-decoration: none; transition: color 0.2s; } .erphp-search-captcha a:hover { color: #333; } </style> </head> <body> <div class="erphp-search-captcha"> <p>为了防止搜索机器人,需要进行人机验证。</p> <form action="" method="post"><?php echo $num1;?> + <?php echo $num2;?> = <input type="text" name="result" required /> <button type="submit">验证</button></form> </div> <a href="<?php echo home_url();?>">返回首页</a> </body> </html> <?php exit; } } } } add_action( 'parse_query', 'esc_search_captcha' );
以上代码可实现搜索人机验证,但是存在一个问题,管理员搜索也会验证,我们需要添加一个权限判断,如果当前用户是管理员,则直接进行搜索,不进行人机验证
下面是完整代码,增加排除管理员,代码登陆可见
这段代码会在搜索时拦截查询操作,如果用户没有通过人机验证,则会输出美化过的人机验证界面,要求用户输入正确的计算结果才能进行搜索。界面使用了灰色背景和简洁的样式,更加美观大方。