Get the App
SLTechnology News&Howtos  ›  Servers  › 

The method of disabling WordPress foreground search function

Shulou Source: shulou.com Published: 2022-06-02 05:08:29 09月29日 Update

This article mainly introduces the method of disabling WordPress foreground search function, which has certain reference value, and friends who need it can refer to it. I hope you all have a lot to gain after reading this article. Let's take a look at it together.

WordPress comes with a weak and inefficient search function, and if someone takes advantage of this flaw to initiate a lot of search requests, your server is likely to go down. We can use Baidu, 360, etc. to achieve site search, and prohibit the front desk from using WordPress's own search function, add the following code to the functions.php file of the current theme.

code

//disable WordPress foreground search function disable_search( $query, $error = true ) {if (is_search() && ! is_admin()) {$query->is_search = false;$query->query_vars['s'] = false;$query->query['s'] = false;if ( $error = true )//Error page displayed after executing search// $query->is_home = true; //Go to home page $query->is_404 = true;//Go to page 404}}add_action ( 'parse_query', 'disable_search' );add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

Note:

You can choose the error page displayed after performing the search, whether to jump to 404 or the home page of the website.

Original code from the network, does not support PHP 7.2, modified to support higher versions of PHP.

Thank you for reading this article carefully, I hope Xiaobian shares the method of disabling WordPress foreground search function to help everyone, but also hope that everyone supports more, pay attention to industry information channels, find problems, detailed solutions waiting for you to learn!

Tags: Search function foreground method code article support error page home page theme value content at the same time efficiency file friend server version ribbon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Shulou Technology Apple MySQL