WordPress自带的搜索功能有两个问题:一个就是搜索过多会造成Wordpress服务器数据库查询压力过大,从而给服务器带来过大的负载压力;第二个就是有坏人会利用Wordpress搜索功能进行恶意搜索,会造成搜索引擎收录搜索结果。
解决这两个问题一个最好的办法就是给Wordpress自带的搜索加一个验证码,阻止那些机器搜索行为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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{color: #333;text-align: center;font-size: 16px;} .erphp-search-captcha{margin: 50px auto 15px;max-width: 250px;width: 100%;padding: 40px 20px;border: 1px solid #ddd;text-align: center;border-radius: 5px;} .erphp-search-captcha form{margin: 0} .erphp-search-captcha input{border: none;border-bottom: 1px solid #666;width: 50px;text-align: center;font-size: 16px;} .erphp-search-captcha input:focus{outline: none;} .erphp-search-captcha button{border: none;background: transparent;color: #ff5f33;cursor: pointer;} .erphp-search-captcha button:focus{outline: none;} a{color: #000;font-size: 12px;} </style> </head> <body> <div class = "erphp-search-captcha" > <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' ); |
LOFI.ICU 已回答的问题 2023年8月29日
是加在你的主题下的funtion.php这个里面。