wp_list_tables bulk actions

时间:2012-12-17 作者:fefe

如何在扩展的WP\\U List\\U表中触发批量操作。

我一直在将以下批量操作添加到may table的选择框中,但在Apply上不会发生任何事情

下面是我如何添加批量操作的

function get_bulk_actions() {
  $actions = array(

        \'delete\'    => \'Delete\',

        \'parsing\'    => \'Parsen\'
  );

  return $actions;
}
下面是复选框列

function column_cb($item) {

    return sprintf(

            \'<input type="checkbox" name="record[]" value="%d" />\', $item[\'Round\']
        );    

}

3 个回复
最合适的回答,由SO网友:Ralf912 整理而成

如果添加批量操作,则必须对此操作作出反应。简单地添加函数没有任何作用,您必须调用它:

class WPSE_List_Table extends WP_List_Table
{
    public function __construct() {

        parent::__construct(
            array(
                \'singular\' => \'singular_form\',
                \'plural\'   => \'plural_form\',
                \'ajax\'     => false
            )
        );

    }

    public function prepare_items() {

        $columns  = $this->get_columns();
        $sortable = $this->get_sortable_columns();
        $hidden   = array( \'id\' );

        $this->_column_headers = array( $columns, $hidden, $sortable );

        $this->process_bulk_action();

    }

    public function get_columns() {

        return array(
            \'cb\'    => \'<input type="checkbox" />\', // this is all you need for the bulk-action checkbox
            \'id\'    => \'ID\',
            \'date\'  => __( \'Date\', \'your-textdomain\' ),
            \'title\' => __( \'Title\', \'your-textdomain\' ),
        );

    }

    public function get_sortable_columns() {

        return array(
            \'date\'  => array( \'date\', false ),
            \'title\' => array( \'title\', false ),
        );

    }

    public function get_bulk_actions() {

        return array(
                \'delete\' => __( \'Delete\', \'your-textdomain\' ),
                \'save\'   => __( \'Save\', \'your-textdomain\' ),
        );

    }

    public function process_bulk_action() {

        // security check!
        if ( isset( $_POST[\'_wpnonce\'] ) && ! empty( $_POST[\'_wpnonce\'] ) ) {

            $nonce  = filter_input( INPUT_POST, \'_wpnonce\', FILTER_SANITIZE_STRING );
            $action = \'bulk-\' . $this->_args[\'plural\'];

            if ( ! wp_verify_nonce( $nonce, $action ) )
                wp_die( \'Nope! Security check failed!\' );

        }

        $action = $this->current_action();

        switch ( $action ) {

            case \'delete\':
                wp_die( \'Delete something\' );
                break;

            case \'save\':
                wp_die( \'Save something\' );
                break;

            default:
                // do nothing or something else
                return;
                break;
        }

        return;
    }

}
在中prepare_items()我们打电话process_bulk_action(). 因此,在您的后端页面上,您将看到如下内容:

$table = new WPSE_List_Table();

printf( \'<div class="wrap" id="wpse-list-table"><h2>%s</h2>\', __( \'Your List Table\', \'your-textdomain\' ) );

echo \'<form id="wpse-list-table-form" method="post">\';

$page  = filter_input( INPUT_GET, \'page\', FILTER_SANITIZE_STRIPPED );
$paged = filter_input( INPUT_GET, \'paged\', FILTER_SANITIZE_NUMBER_INT );

printf( \'<input type="hidden" name="page" value="%s" />\', $page );
printf( \'<input type="hidden" name="paged" value="%d" />\', $paged );

$table->prepare_items(); // this will prepare the items AND process the bulk actions
$table->display();

echo \'</form>\';

echo \'</div>\';
首先,创建list table类的实例。然后创建一个公式并调用prepare_items(). 通过此调用,将处理批量操作,因为我们调用了方法process_bulk_action() 在…内prepare_items().

在上面的示例中,我们使用post 作为发送数据的方法。因此,如果我们不想在类内处理批量操作(或出于其他原因),我们可以从post数组中获取批量操作。

// this is the top bulk action!!
$action = ( isset( $_POST[\'action\'] ) ) ?
    filter_input( INPUT_POST, \'action\', FILTER_SANITIZE_STRIPPED ) : \'default_top_bulk_action\';

// this is the bottom bulk action!!
$action2 = ( isset( $_POST[\'action2\'] ) ) ? 
    filter_input( INPUT_POST, \'action2\', FILTER_SANITIZE_STRIPPED ) : \'default_bottom_bulk_action\';

switch ( $action ) {}
switch ( $action2 ) {}
您可以从post/get数组中的任意位置获取批量操作(取决于发送数据所使用的方法)。

SO网友:samjco

WP List w/ bulk actions using an "Array" DATA

https://wpengineer.com/2426/wp_list_table-a-step-by-step-guide/ (将其作为插件)https://gist.github.com/Latz/7f923479a4ed135e35b2 (对于functions.php)

WP List w/ bulk actions using an "SQL" DATA

https://www.smashingmagazine.com/2011/11/native-admin-tables-wordpress/ (对于functions.php)

干杯

SO网友:Jan

这是我在几个网站上发现的。它对我仍然不起作用,但在我的自定义列表表中仍然有示例数据时,它就起作用了。

  function process_bulk_action() {

    //Detect when a bulk action is being triggered...
    if( \'delete\'===$this->current_action() ) {
        wp_die(\'Items deleted (or they would be if we had items to delete)!\');
    }

}

结束