WP_LIST_TABLE搜索框未显示

时间:2013-12-20 作者:garydefty

我正在使用wordpress 3.8和一个从WP_List_Table. 为什么没有搜索表单?

My Plugin admin page (using WP_List_Table)

插件类中的我的代码:

function __construct()
            {
                register_activation_hook( __FILE__, array( $this, \'swb_plugin_install\' ) );
                register_deactivation_hook( __FILE__, array( $this, \'swb_plugin_deactivate\' ) );
                register_uninstall_hook( __FILE__, array( $this, \'swb_plugin_remove\' ) );

                add_action( \'wp_head\', array( $this, \'swb_header\' ) );
                add_action( \'admin_menu\', array( $this, \'swb_admin_page\' ) );
                add_action( \'admin_enqueue_scripts\', array( $this, \'swb_admin_script\' ) );
            }

    function swb_admin_resellers()
        {
            $list_obj = new SWB_Resellers_List_Table();
            if( isset($_POST[\'s\']) ){
                $list_obj->prepare_items($_POST[\'s\']);
            } else {
                $list_obj->prepare_items();
            }
            echo \'<div class="wrap"><h2>Resellers List</h2>\';
            ?>
            <form method="post">
                <input type="hidden" name="page" value="<?php echo $_REQUEST[\'page\'] ?>" />
                <?php $list_obj->search_box(\'search reseller(s)\', \'search_id\'); ?>
                <?php $list_obj->display(); ?>
            </form></div>
            <?php
        }

function swb_admin_page()
        {
            add_menu_page( \'\', \'Smarts Web Builder\', \'activate_plugins\', \'swb_plugin_root\', array( $this, \'swb_admin_settings\' ) );
            add_submenu_page( \'swb_plugin_root\', \'SWB General Setting\', \'General Settings\', \'activate_plugins\', \'swb_plugin_root\', array( $this, \'swb_admin_settings\' ) );
            add_submenu_page( \'swb_plugin_root\', \'SWB Resellers List\', \'Resellers\', \'activate_plugins\', \'swb_resellers\', array( $this, \'swb_admin_resellers\' ) );
            add_submenu_page( \'swb_plugin_root\', \'SWB Licenses List\', \'Licenses\', \'activate_plugins\', \'swb_licenses\', array( $this, \'swb_admin_licenses\' ) );
            add_submenu_page( \'swb_plugin_root\', \'SWB Customers List\', \'Customers\', \'activate_plugins\', \'swb_customers\', array( $this, \'swb_admin_customers\' ) );
            add_submenu_page( \'swb_plugin_root\', \'SWB Themes\', \'Manage Themes\', \'activate_plugins\', \'swb_themes\', array( $this, \'swb_admin_themes\' ) );
            add_submenu_page( \'swb_plugin_root\', \'SWB Plugins\', \'Manage Plugins\', \'activate_plugins\', \'swb_plugins\', array( $this, \'swb_admin_plugins\' ) );
        }
扩展WP\\u List\\u表的类:

if(!class_exists( \'SWB_Resellers_List_Table\' ) )
{
    class SWB_Resellers_List_Table extends WP_List_Table
    {
        function __construct()
        {
            parent::__construct(array(
                \'singular\' => \'wp_list_text_link\',
                \'plural\' => \'wp_list_test_links\',
                \'ajax\' => false
            ));
        }

        function get_columns()
        {
            return $column = array(
                \'col_name\' => __(\'Real Name\'),
                \'col_username\' => __(\'Username\'),
                \'col_contact_no\' => __(\'Contact No.\'),
                \'col_email\' => __(\'Email\'),
                \'col_active\' => __(\'Is Active?\'),
                \'col_registered_time\' => __(\'Registered\'),
                \'col_modified_time\' => __(\'Last Modified\')
            );
        }

        public function get_sortable_columns()
        {
            return $sortable = array(
                \'col_name\' => array(
                    \'name\',
                    false
                ),
                \'col_username\' => array(
                    \'username\',
                    false
                ),
                \'col_email\' => array(
                    \'email\',
                    false
                ),
                \'col_active\' => array(
                    \'active\',
                    false
                ),
                \'col_registered_time\' => array(
                    \'registered_time\',
                    false
                ),
                \'col_modified_time\' => array(
                    \'modified_time\',
                    false
                )
            );
        }

        function prepare_items($search=\'\')
        {
            global $wpdb;

            $table = $wpdb->prefix . SWB_TABLE_RESELLER;
            $join1 = $wpdb->prefix . "wlm_user_options";
            $join2 = $wpdb->users;

            $short_table = "T1";
            $short_join1 = "T2";
            $short_join2 = "T3";

            $column = "{$short_table}.reseller_id reseller_id,
                {$short_table}.user_id user_id,
                {$short_table}.active active,
                {$short_table}.modified_time modified_time,
                (SELECT {$short_join1}.option_value FROM {$join1} {$short_join1} WHERE {$short_join1}.option_name=\'custom_real_name\' AND {$short_join1}.user_id={$short_table}.user_id) name,
                (SELECT {$short_join1}.option_value FROM {$join1} {$short_join1} WHERE {$short_join1}.option_name=\'custom_contact_no\' AND {$short_join1}.user_id={$short_table}.user_id) contact_no,
                {$short_join2}.user_login username,
                {$short_join2}.user_email email,
                {$short_join2}.user_registered registered_time";
            $on1 = "{$short_join2}.ID={$short_table}.user_id";

            $where = "";
            $where .= !empty($_POST[\'name\']) ? "name=" . trim($_POST[\'name\']) : null;
            $where .= !empty($_POST[\'username\']) ? "username=" . trim($_POST[\'username\']) : null;
            if(!empty($search)){
                if( !empty($where) )    $where .= " AND ";
                $where = "(name LIKE \'%{$search}%\' OR username LIKE \'%{$search}%\')";
            }
            if( !empty($where) )    $where = " WHERE " . $where;

            $ordercolumn = !empty($_GET[\'orderby\']) ? trim($_GET[\'orderby\']) : null;
            $orderdirection = !empty($_GET[\'order\']) ? trim($_GET[\'order\']) : null;
            $orderby = "{$ordercolumn} {$orderdirection}";
            if( !empty($orderby) )  $orderby = "ORDER BY " . $orderby;

            $query = "SELECT {$column} FROM {$table} {$short_table} JOIN {$join1} {$short_join1} JOIN {$join2} {$short_join2} ON {$on1}{$where}{$order_by}";

            $totalitems = $wpdb->query($query);

            $perpage = 10;

            $paged = !empty($_GET["paged"]) ? intval($_GET["paged"]) : \'\';

            if (empty($paged) || !is_numeric($paged) || $paged <= 0) {
            $paged = 1;
            }

            $totalpages = ceil($totalitems / $perpage);

            if (!empty($paged) && !empty($perpage)) {
            $offset = ($paged - 1) * $perpage;
            $query .= \' LIMIT \' . ( int ) $offset . \',\' . ( int ) $perpage;
            }

            // Register page
            $this->set_pagination_args(
                array(
                    "total_items" => $totalitems,
                    "total_pages" => $totalpages,
                    "per_page" => $perpage
                )
            );

            // Register column
            $columns               = $this->get_columns();
            $hidden                = array();
            $sortable              = $this->get_sortable_columns();
            $this->_column_headers = array(
                $columns,
                $hidden,
                $sortable
            );
            // var_dump($this);
            // echo $query;

            // Fetch items
            $this->items = $wpdb->get_results($query);

        }
    }

    function display_rows()
    {
        $records = $this->items;
        $columns = null;
        $hidden  = null;
        list($columns, $hidden) = $this->get_column_info();

        if (!empty($records)) {
            foreach ($records as $rec) {
                echo \'<tr id="swb-reseller-row-\' . $rec->reseller_id . \'">\';
                foreach ($columns as $column_name => $column_display_name) {
                $class = "class=\'{$column_name} column-{$column_name}\'";
                    $style = "";
                    if (in_array($column_name, $hidden))
                        $style = \' style="display:none;"\';
                    $attributes = $class . $style;

                    switch ($column_name) {
                        case "col_name":
                            echo \'<td \' . $attributes . \'>\' . stripslashes($rec->name) . \'</td>\';
                            break;
                        case "col_username":
                            echo \'<td \' . $attributes . \'>\' . stripslashes($rec->username) . \'</td>\';
                            break;
                        case "col_contact_no":
                            echo \'<td \' . $attributes . \'>\' . stripslashes($rec->contact_no) . \'</td>\';
                            break;
                        case "col_email":
                            echo \'<td \' . $attributes . \'>\' . stripslashes($rec->email) . \'</td>\';
                            break;
                        case "col_active":
                            $active = ( intval($rec->active) == 1 ) ? \'Yes\' : \'No\';
                            echo \'<td \' . $attributes . \'>\' . $active . \'</td>\';
                            break;
                        case "col_registered_time":
                            echo \'<td \' . $attributes . \'>\' . date("F j, Y g:i a", stripslashes($rec->registered_time)) . \'</td>\';
                            break;
                        case "col_modified_time":
                            echo \'<td \' . $attributes . \'>\' . date("F j, Y g:i a", stripslashes($rec->modified_time)) . \'</td>\';
                            break;
                    }
                }
                echo \'</tr>\';
            }
        }
    }
}
我不知道为什么搜索框没有显示出来。缺少什么?

4 个回复
SO网友:axentmedia

您必须使用$list_obj->prepare_items() 之前$list_obj->search_box(\'Search\', \'search\') 作用

示例:

                        <form method="post">
                            <input type="hidden" name="page" value="<?php echo $_REQUEST[\'page\']; ?>" />
                            <?php $this->sales_obj->prepare_items();
                            $this->sales_obj->search_box(\'Search\', \'search\');
                            $this->sales_obj->display(); ?>
                        </form>

SO网友:kevado

我通过使用以下内容覆盖search\\u box()函数来解决此问题:

 public function search_box( $text, $input_id ) { ?>
    <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, false, array(\'id\' => \'search-submit\') ); ?>
  </p>
<?php }
将此函数放在自定义WP\\U List\\U Tables类中,它将解决此问题。

SO网友:fumigato

查看“wp\\u list\\u table”搜索框:

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, false, array(\'id\' => \'search-submit\') ); ?>
</p>
<?php
}
Theif(empty($_REQUEST[\'s\']) && !$this->has_items() 部件可能会阻塞search_box 外貌

SO网友:ewroman

如果在表中添加一些数据,您将看到该框。如果它说"No items found" WordPress不显示该框。添加一些数据,将出现框。

结束

相关推荐

GoPardy托管wp-admin问题

我在GoDaddy托管帐户(Windows)上的/wp admin登录页有一个大问题。如果访问以下url,我将无法登录。我再次被引导到登录页面。域。com/wp admin如果使用以下URL,我可以登录。域。com/wp admin/(末尾带正斜杠)域。com/admin域。com/login我将要使用的域指向wordpress的安装,它位于根目录的子目录下。禁用permalinks(删除了web.config,我想是windows的.htaccess)禁用的插件在数据库中验证了站点URL(它不包含额外的