0

I am currently trying to add a WP_List_Table to a metabox in the edit page of a custom type. The code is currently

    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(); 

If I uncomment $postTable->display(), the update button of the edit page ceases to work correctly. It does not save changes anymore, and instead just redirects me to wp-admin/edit.php, the list of all posts. This behaviour still remains if I comment out all action-related methods in the Post_Table class.

What causes this behaviour and how to avoid it?

1

It's nothing to do with the class itself - you are using the reserved input name post, which is conflicting with WordPress core:

<input type="checkbox" name="post[]" value="%s" />

Change it something unique to your theme/plugin e.g. name="my_plugin_ids[]"

  • Changing the name or commenting out that function still produces the same problem. – Slereah Apr 27 '16 at 11:50
  • Hmm, must be one of the other inputs that WP_List_Table kicks out (methods called by display e.g. display_tablenav). List table class was never really designed to be used on an edit post page. – TheDeadMedic Apr 27 '16 at 14:27
  • What would be a good alternative to put a list in a custom metabox? – Slereah Apr 27 '16 at 14:30
  • Based on what it looks like you're trying to do, I would just output an HTML table with PHP, using core WordPress classes for styling e.g. <table class="widefat fixed striped"><thead>...</thead><tbody>...</tbody></table> – TheDeadMedic Apr 27 '16 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.