如何对WP_LIST_TABLE(核心类)进行自定义批量操作

时间:2012-08-02 作者:Seeker

我正在使用WP_List_Table 类和我为以下操作添加了链接:

function get_bulk_actions() {
        $actions = array();

        $actions[\'approv\'] =\'<a href="#">\'.__( \'Approve\' ).\'</a>\';
                $actions[\'reject\'] = \'<a href="#">\'.__( \'Reject\' ).\'</a>\';
                $actions[\'delete\'] = \'<a href="#">\'.__( \'Delete\' ).\'</a>\';
                $actions[\'view\'] = \'<a href="#">\'.__( \'View\' ).\'</a>\';


        return $actions;
    }
然后我在display_row() 功能如下:

$actions = $this->get_bulk_actions();
echo $this->row_actions( $actions );
现在我想知道如何将自定义操作添加到我创建的链接中。是否有任何地方可以将变量发送到页面?要对这些链接进行DB处理并重新填充WP_List_Table 这是在我执行操作之前创建的。

更新1以下是WP_List_Table 类别:

<?php
// Our class extends the WP_List_Table class, so we need to make sure that it\'s there
if( ! class_exists( \'WP_List_Table\' ) ) {
    require_once( ABSPATH . \'wp-admin/includes/class-wp-list-table.php\' );
}
class Link_List_Table extends WP_List_Table {

    /**
     * Constructor, we override the parent to pass our own arguments
     * We usually focus on three parameters: singular and plural labels, as well as whether the class supports AJAX.
     */
     function __construct() {
         parent::__construct( array(
        \'singular\'=> \'wp_list_text_link\', //Singular label
        \'plural\' => \'wp_list_test_links\', //plural label, also this well be one of the table css class
        \'ajax\'  => true //We won\'t support Ajax for this table
        ) );
     }
/**
 * Define the columns that are going to be used in the table
 * @return array $columns, the array of columns to use with the table
 */
function get_columns() {
    return $columns= array(
        \'col_person_name\' => \'Name\',
                \'col_business_name\' => \'Bussiness Name\' ,
                \'col_person_email\' => \'Email\' , 
                \'col_pserson_phone\' => \'Contact Number\',
                \'col_request_status\' => \'Status\',
                \'col_is_deleted\' => \'Deleted\'
    );
}

/**
 * Prepare the table with different parameters, pagination, columns and table elements
 */
function prepare_items() {
    global $wpdb, $_wp_column_headers;
    $screen = get_current_screen();

    /* -- Preparing your query -- */
        $query = "SELECT * FROM approval_requests";

    /* -- Ordering parameters -- */
        //Parameters that are going to be used to order the result
        $orderby = !empty($_GET["orderby"]) ? mysql_real_escape_string($_GET["orderby"]) : \'ASC\';
        $order = !empty($_GET["order"]) ? mysql_real_escape_string($_GET["order"]) : \'\';
        if(!empty($orderby) & !empty($order)){ $query.=\' ORDER BY \'.$orderby.\' \'.$order; }

    /* -- Pagination parameters -- */
        //Number of elements in your table?
        $totalitems = $wpdb->query($query); //return the total number of affected rows
        //How many to display per page?
        $perpage = 20;
        //Which page is this?
        $paged = !empty($_GET["paged"]) ? mysql_real_escape_string($_GET["paged"]) : \'\';
        //Page Number
        if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }
        //How many pages do we have in total?
        $totalpages = ceil($totalitems/$perpage);
        //adjust the query to take pagination into account
        if(!empty($paged) && !empty($perpage)){
            $offset=($paged-1)*$perpage;
            $query.=\' LIMIT \'.(int)$offset.\',\'.(int)$perpage;
        }

    /* -- Register the pagination -- */
        $this->set_pagination_args( array(
            "total_items" => $totalitems,
            "total_pages" => $totalpages,
            "per_page" => $perpage,
        ) );
        //The pagination links are automatically built according to those parameters

    /* -- Register the Columns -- */
        $columns = $this->get_columns();
                $hidden = array();
                $sortable = array();
                $this->_column_headers = array($columns, $hidden, $sortable);
    /* -- Fetch the items -- */
        $this->items = $wpdb->get_results($query);
}


/**
 * Display the rows of records in the table
 * @return string, echo the markup of the rows
 */


function display_rows() {

    //Get the records registered in the prepare_items method
    $records = $this->items;
          list( $columns, $hidden ) = $this->get_column_info();

          //print_r($records);
    //Loop for each record
    if(!empty($records)){foreach($records as $rec){

        //Open the line
        echo \'<tr id="record_\'.$rec->sr_no.\'">\';
        foreach ( $columns as $column_name => $column_display_name ) {

            //Style attributes for each col
            $class = "class=\'$column_name column-$column_name\'";
            $style = "";
            if ( in_array( $column_name, $hidden ) ) $style = \' style="display:none;"\';
            $attributes = $class . $style;

            //edit link



            //Display the cell
            switch ( $column_name ) {
                case "col_person_name": 
                                    echo \'<td \'.$attributes.\'>\'.stripslashes($rec->person_name);
                                       $actions = $this->get_bulk_actions();

                    echo $this->row_actions( $actions );
                                     echo \'</td>\'; 
                                break;
                case "col_business_name": echo \'<td \'.$attributes.\'>\'.stripslashes($rec->business_name).\'</td>\'; break;
                case "col_person_email": echo \'<td \'.$attributes.\'>\'.$rec->person_email.\'</td>\'; break;
                case "col_pserson_phone": echo \'<td \'.$attributes.\'>\'.$rec->pserson_phone.\'</td>\'; break;
                                case "col_request_status": echo \'<td \'.$attributes.\'>\'.$rec->request_status.\'</td>\'; break;
                case "col_is_deleted": echo \'<td \'.$attributes.\'>\'.$rec->is_deleted.\'</td>\'; break;
            }
        }

        //Close the line
        echo\'</tr>\';
    }}
}

function get_bulk_actions() {
        $actions = array();

        $actions[\'approv\'] =\'<a href="#">\'.__( \'Approve\' ).\'</a>\';
                $actions[\'reject\'] = \'<a href="#">\'.__( \'Reject\' ).\'</a>\';
                $actions[\'delete\'] = \'<a href="#">\'.__( \'Delete\' ).\'</a>\';
                $actions[\'view\'] = \'<a href="#">\'.__( \'View\' ).\'</a>\';


        return $actions;
    }



}

function show_table(){

$wp_list_table = new Link_List_Table();
$wp_list_table->prepare_items();

//Table of elements
$wp_list_table->display();


}

?>

1 个回复
SO网友:Latz

wpengineer的本教程可能有助于:

WP_List_Table – a step by step guide: Bulk actions

结束

相关推荐

Bulk User Deletion

我创建了一个WordPress 3.0站点,启用了多站点,并安装了BuddyPress插件(每个插件的最新可用版本)。最初,该网站没有启用验证码,现在创建了数百个垃圾邮件帐户。默认情况下,用户的超级管理面板一次仅显示15个用户。我需要删除几百个用户,一页一页地浏览这个页面很单调。是否有可以帮助批量删除用户的插件?我希望能够一次选择和删除几百个用户(或者更好地通过用户名进行regex搜索)。谢谢