4

How can you disable the post search field "Search posts" - for non-admin roles in WP dashboard?

Image attached:

enter image description here

In my case, the non-admin role is a customer. The customer cannot and must not edit or delete any further posts which belong to other parties. Those posts the customer can view on pages as a common viewer. It's a customer. So I am asking why to show up that search field. I want to disable it.

(Example: if you advertise a flower vase, do you would then want to search for flower vases from others? Maybe some want it - but I don't want to offer that feature)

(Incidental things not relevant to answering the question: BTW, in case someone is asking: I cannot assign the common WP customer role because it does not meet the requirements. BTW, The customer is eligible to delete and view his post. Editing the own posts is possible with Elementor but not with the common WP link)

Attempts:

I tried 4 code snippets from the plugin "Code Snippets" by WPCode, but the search field "Search posts" in WP dashboard (posts) for non-admin roles stays!

// Prevent search queries

add_action(
'parse_query',
function ( $query, $error = true ) {
    if ( is_search() && ! is_admin() ) {
        $query->is_search       = false;
        $query->query_vars['s'] = false;
        $query->query['s']      = false;
        if ( true === $error ) {
            $query->is_404 = true;
        }
    }
},
15,
2

);

// Remove the Search Widget

add_action(
'widgets_init',
function () {
    unregister_widget( 'WP_Widget_Search' );
});

// Remove the search form

 add_filter( 'get_search_form', '__return_empty_string', 999 );

// Remove the core search block

add_action(
'init',
function () {
    if ( ! function_exists( 'unregister_block_type' ) || ! class_exists( 'WP_Block_Type_Registry' ) ) {
        return;
    }
    $block = 'core/search';
    if ( WP_Block_Type_Registry::get_instance()->is_registered( $block ) ) {
        unregister_block_type( $block );
    }
});
4
  • 1
    Do you only need to hide the search box, or the search functionality needs to be disabled as well? I'm saying this because even if you remove the search box, someone can still manually use example.com/wp-admin/edit.php?s=SEARCH_TERM to actually do the search.
    – Fayaz
    Feb 5 at 14:31
  • Good hint. I don't want to make the decision yet. Could you post the two different codes.
    – vallespir
    Feb 5 at 14:57
  • 1
    Code is not straight forward for this, I'll have to make time to look into it. However, you should update the question and explain why you need this. Search is a basic function. Users who'll see the posts and edit the posts, will be able to check all the posts manually anyway. So why do you really need it? What purpose does it serve? I'm saying this because perhaps someone can provide a better solution if the real purpose is known.
    – Fayaz
    Feb 5 at 16:28
  • I updated the question
    – vallespir
    Feb 5 at 18:14

1 Answer 1

3

There is no straight forward documented way of removing the search box in the admin panel's Post dashboard (wp-admin/edit.php).

However, it's still possible by extending WP_Posts_List_Table class.

Additionally, it makes sense that if you remove the search box, you'd also like to disable the search capability all together (based on your requirement).

In that case, you may redirect any search request to the default wp-admin/edit.php page. That'll effectively disable any manual search attempts.

Following is a fully functional example plugin code that demonstrates how this can be implemented:

<?php
/**
 * Plugin Name: @fayaz.dev Remove post search in dashboard
 * Description: Remove post search option in wp-admin/edit.php for users who don't have the capability to edit other's posts. 
 * Author: Fayaz Ahmed
 * Version: 1.0.0
 * Author URI: https://fayaz.dev/
 **/

namespace Fayaz\dev;

function init_search_box_removal() {
    if( is_admin() && ! wp_doing_ajax() && ! current_user_can( 'edit_others_posts' ) ) {
        // disable search capability
        if( isset( $_REQUEST['s'] ) ) {
            wp_safe_redirect( admin_url( 'edit.php' ) );
            exit;
        }

        // remove the search box
        add_filter( 'wp_list_table_class_name', '\Fayaz\dev\define_wp_posts_list_table', 10, 2 );
    }
}
add_action( 'set_current_user', '\Fayaz\dev\init_search_box_removal' );

function define_wp_posts_list_table( $class_name, $args ) { 
    if( $class_name === 'WP_Posts_List_Table' ) {
        class WP_Posts_List_Table_Search extends \WP_Posts_List_Table {
            // this just overrides WP_List_Table::search_box() method.
            // the overriding function here does nothing,
            // hence it effectively removes the search box
            public function search_box( $text, $input_id ) {}
        }
        return '\Fayaz\dev\WP_Posts_List_Table_Search';
    }
    return $class_name;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.