显示WP_LIST_TABLE时更新按钮的奇怪行为

时间:2016-04-27 作者:Slereah

我当前正在尝试将WP\\U List\\U表添加到自定义类型的编辑页面中的元框中。代码当前为

    class Post_Table extends WP_List_Table {

    public function __construct() 
    {
        parent::__construct([
            \'singular\' => __( \'Post\', \'sp\' ),
            \'plural\'   => __( \'Posts\', \'sp\' ),
            \'ajax\'     => false 
        ]);
    }

    function get_columns(){
        $columns = array(
        \'cb\'    => \'<input type="checkbox" />\',
        \'title\' => \'Title\',
        );
        return $columns;
    }

    function prepare_items() {
        $columns = $this->get_columns();
        $hidden = array();
        $sortable = array();
        $this->_column_headers = array($columns, $hidden, $sortable);
        $this->items = array();
    }

    function add_item($item)
    {
        array_push($this->items, $item);
    }

    function column_default( $item, $column_name ) {
        switch( $column_name ) { 
            case \'title\':
                return $item[ $column_name ];
            default:
                return print_r( $item, true ) ; 
      }
    }

    function column_title($item) {
        $actions = array(
            \'delete\'    => sprintf(\'<a >Delete</a>\',$_REQUEST[\'page\'],\'delete\',$item[\'ID\']),
        );

        return sprintf(\'%1$s %2$s\', $item[\'title\'], $this->row_actions($actions) );
    }
    function get_bulk_actions() {
        $actions = array(
            \'delete\'    => \'Delete\'
        );
        return $actions;
    }

    function process_bulk_action() {        
        if( \'delete\'===$this->current_action() ) 
        {
            update_post_meta($post->ID, \'_page_list\', array());
        }        
    }

    function column_cb($item) {
        return sprintf(
            \'<input type="checkbox" name="post[]" value="%s" />\', $item[\'ID\']
        );    
    }


}

$postTable = new Post_Table();
$postTable->prepare_items(); 
$n = 1;

foreach($page_list as $page)
{
    $postTable->add_item(array(
        \'ID\' => $n,
        \'title\' => get_the_title(get_post($page))
        ));
    $n++;
}

//$postTable->display(); 
如果我取消注释$postTable->display(),编辑页面的更新按钮将停止正常工作。它不再保存更改,而是将我重定向到wp admin/edit。php,所有帖子的列表。如果我注释掉Post\\u Table类中所有与操作相关的方法,这种行为仍然存在。

是什么导致这种行为以及如何避免这种行为?

1 个回复
SO网友:TheDeadMedic

这与类本身无关-您使用的是保留的输入名称post, 与WordPress核心冲突:

<input type="checkbox" name="post[]" value="%s" />
将其更改为您的主题/插件独有的内容,例如。name="my_plugin_ids[]"