WP_List_tables::search_box not working

So i have extended the WP_list_tables class and i can get everything working accept the search box. The code for displaying my table is below

<div class="wrap"><h2>Request Log</h2>
<form method="post">
    <input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
    <?php if(isset($myRequestTable)) : ?>
        <?php $myRequestTable->search_box('Search', 'search_id'); ?>
        <?php $myRequestTable->prepare_items() ?>
        <?php $myRequestTable->display() ?>
    <?php endif; ?>
</form>

Read More

As i say this all work accept from the search $myRequestTable->search_box('Search', 'search_id')

after doing some looking into it I see that the the method this calls (below) does a check that fails if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
now $_REQUEST['s'] seems to be a parameter that is set by the search box itself so this means that it will never display until its already displayed :/

public function search_box( $text, $input_id ) {
    if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
        return;

    $input_id = $input_id . '-search-input';

    if ( ! empty( $_REQUEST['orderby'] ) )
        echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
    if ( ! empty( $_REQUEST['order'] ) )
        echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
    if ( ! empty( $_REQUEST['post_mime_type'] ) )
        echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
    if ( ! empty( $_REQUEST['detached'] ) )
        echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
?>
<p class="search-box">
<label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
<input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
<?php submit_button( $text, 'button', '', false, array('id' => 'search-submit') ); ?>
</p>
<?php
}

I could be wrong but if i’m not and $_REQUEST[‘s’] needs to be set to display the search box, where does this get set or do i need to set somehow?

I did some searching and found this:
https://wordpress.stackexchange.com/questions/127138/wp-list-table-search-box-does-not-show

Some one does point out the problem but not a solution the OP resolved by overriding the method but I feel that there must be a way to use the method that WP provides. can anyone help?

Related posts

Leave a Reply

1 comment

  1. Ok So I did get this wrong. I wasn’t reading the WP function correctly is actually requires $_REQUEST['s'] to be empty. It was failing at !$this->has_items() and this was because of the order i was calling the methods on the display. Because the items had not been prepared yet the the $items property on the class was empty. changed the order the functions are called and this started to work perfectly.

    <div class="wrap"><h2>Request Log</h2>
    <form method="post">
    <input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
    <?php if(isset($myRequestTable)) : ?>
        <?php $myRequestTable->prepare_items()   ?>
        <?php $myRequestTable->search_box('Search', 'search_id'); //Needs To be called after $myRequestTable->prepare_items() ?>
        <?php $myRequestTable->display() ?>
    <?php endif; ?>
    </form>
    

    I feel a bit stupid but at least this is resolved!