Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - Disable Search Function

I've added the following code to functions.php to disable archived/crawled/url searching on my wordpress site for performance reasons:

function disable_search( $query, $error = true ) {
  if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;
// to error
if ( $error == true )
$query->is_404 = true;
  }
}

add_action( 'parse_query', 'disable_search' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

This works perfectly to prevent searching, but also prevents searching for posts in the admin area.

Is there a way to disable searches for users but still allow the admins to search?

Currently, my workaround is to remove this code, perform my necessary search, then paste it back.

like image 662
Justin Avatar asked Sep 19 '26 19:09

Justin


1 Answers

Did you try is_admin function? It will return true if the Dashboard or the administration panel is attempting to be displayed. So put this function to your code, it will not fire in Dashboard:

function disable_search($query, $error = true)
{
    if (is_search() && !is_admin()) {
        $query->is_search = false;
        $query->query_vars['s'] = false;
        $query->query['s'] = false;

        // to error

        if ($error == true) $query->is_404 = true;
    }
}

add_action('parse_query', 'disable_search');
add_filter('get_search_form', '__return_null');

Or:

function disable_search($query, $error = true)
{
    if (is_search()) {
        $query->is_search = false;
        $query->query_vars['s'] = false;
        $query->query['s'] = false;

        // to error

        if ($error == true) $query->is_404 = true;
    }
}

if(!is_admin()){
    add_action('parse_query', 'disable_search');
    add_filter('get_search_form', '__return_null');
}
like image 129
Argus Duong Avatar answered Sep 21 '26 15:09

Argus Duong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!