How to disable search function

How can I remove search function (website.com/?s=) in WordPress?

Add this script to your functions file or plugin:

<?php
/**
* Redirect Search to home page
**/
add_action('template_redirect','wpster_search_redirect');
function wpster_search_redirect() {
 global $wp_rewrite;
 if(is_search()&&isset($_GET['s'])) {
  $s = sanitize_text_field($_GET['s']);
  $location = '/';
  $location = home_url($location);
  wp_safe_redirect($location,301);
  exit;
 }
}
?>

And if you want only to block search without parameters, add this script:

<?php
/**
* Disable empty search
**/
add_action('wp', 'wpster_redirect_empty_search');
function wpster_redirect_empty_search() {
  global $wp_query;
  if( isset($_GET['s']) && $_GET['s']=='' ) {
    wp_redirect( home_url() );
    exit;
  }
}
?>

Submit a Comment

Your email address will not be published. Required fields are marked *