Do you want to disable the search feature in WordPress? Sometimes your site may not need the search feature and the search form in your theme may interfere with user experience. In this article, we will show you how to easily disable the search feature in WordPress.
您要禁用WordPress中的搜索功能吗? 有时,您的站点可能不需要搜索功能,主题中的搜索表单可能会干扰用户体验。 在本文中,我们将向您展示如何轻松禁用WordPress中的搜索功能。
为什么以及谁应该禁用WordPress中的搜索功能? (Why and Who Should Disable Search Feature in WordPress?)
Many WordPress websites are simple business websites with a few pages. There is also a growing trend of one-page websites with vertical navigation.
许多WordPress网站是具有几页的简单商务网站 。 具有垂直导航的单页网站也有增长的趋势。
These websites does not have much content which makes search form a novelty item and not a useful feature.
这些网站内容不多,使得搜索成为一个新颖的项目,而不是有用的功能。
It also gives users the impression that there might be some other information that they can’t see and hence the search option. Removing search feature will clean up your website and offer a better user experience.
它还给用户留下了可能还有其他一些看不见的信息,因此还有搜索选项。 删除搜索功能将清理您的网站并提供更好的用户体验。
That being said, let’s take a look at how to easily remove search feature from your WordPress site.
话虽如此,让我们看一下如何轻松地从WordPress网站中删除搜索功能。
方法1.使用插件删除WordPress中的搜索功能 (Method 1. Remove Search Feature in WordPress Using a Plugin)
This method is easier and is recommended for all users.
此方法更简单,建议所有用户使用。
First thing you need to do is install and activate the Disable Search plugin. For more details, see our step by step guide on how to install a WordPress plugin.
您需要做的第一件事是安装并激活“ 禁用搜索”插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。
The plugin works out of the box, and there are no settings for you to configure.
该插件可以直接使用,没有可供您配置的设置。
Upon activation, it will remove search form from your WordPress theme and disable the search widget. If a user directly tried to enter a search query URL, the plugin will return a 404 error page.
激活后,它将从您的WordPress主题中删除搜索表单,并禁用搜索小部件。 如果用户直接尝试输入搜索查询URL,则插件将返回404错误页面 。
Note that this plugin does not affect the search functionality inside the WordPress admin area. You can still search posts and pages inside your WordPress admin.
请注意,此插件不会影响WordPress 管理区域内的搜索功能。 您仍然可以在WordPress管理员中搜索帖子和页面 。
方法2。手动禁用WordPress中的搜索功能 (Method 2. Manually Disable Search Feature in WordPress)
This method requires you to add code to your WordPress files. If you haven’t done this before, then take a look at our guide on how to copy and paste code in WordPress.
此方法要求您将代码添加到WordPress文件中。 如果您以前没有做过,请查看我们的指南, 了解如何在WordPress中复制和粘贴代码 。
You will need to add this code to your theme’s functions.php file or a site-specific plugin.
您将需要将此代码添加到主题的functions.php文件或特定于站点的插件中 。
function wpb_filter_query( $query, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;
if ( $error == true )
$query->is_404 = true;
}
}
add_action( 'parse_query', 'wpb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
function remove_search_widget() {
unregister_widget('WP_Widget_Search');
}
add_action( 'widgets_init', 'remove_search_widget' );
This code will simply redirect all direct or indirect search queries to a 404 page. It will also hide the search form in your WordPress theme.
此代码将直接将所有直接或间接搜索查询重定向到404页面。 它还会将搜索表单隐藏在您的WordPress主题中。
We hope this article helped you learn how to easily disable search feature in WordPress. You may also want to see our mega list of the most wanted WordPress tips, tricks, and hacks.
我们希望本文能帮助您学习如何轻松禁用WordPress中的搜索功能。 您可能还希望查看我们的大型列表,其中包括最需要的WordPress技巧,窍门和hack 。
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-disable-the-search-feature-in-wordpress/