0

I have a custom field which is named clicktripz_object_location. I've created a WP_List_Table with the following code;

if(is_admin())
{
    new UCO_Wp_List_Table();
}

/**
 * UCO_Wp_List_Table class will create the page to load the table
 */
class UCO_Wp_List_Table
{
    /**
     * Constructor will create the menu item
     */
    public function __construct()
    {
        add_action( 'admin_menu', array($this, 'add_menu_uco_list_table_page' ));
    }

    /**
     * Menu item will allow us to load the page to display the table
     */
    public function add_menu_uco_list_table_page()
    {
        add_menu_page( 'Custom UCO', 'Custom UCO', 'manage_options', 'clicktripz-custom-uco.php', array($this, 'list_table_page') );
    }

    /**
     * Display the list table page
     *
     * @return Void
     */
    public function list_table_page()
    {
        $exampleListTable = new UCO_List_Table();
        $exampleListTable->prepare_items();
        ?>
            <div class="wrap">
                <div id="icon-users" class="icon32"></div>
                <h2>Custom UCO</h2>
                <?php $exampleListTable->display(); ?>
            </div>
        <?php
    }
}

// WP_List_Table is not loaded automatically so we need to load it in our application
if( ! class_exists( 'WP_List_Table' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}

/**
 * Create a new table class that will extend the WP_List_Table
 */
class UCO_List_Table extends WP_List_Table
{
    /**
     * Prepare the items for the table to process
     *
     * @return Void
     */
    public function prepare_items()
    {
        $columns = $this->get_columns();
        $hidden = $this->get_hidden_columns();
        $sortable = $this->get_sortable_columns();

        $data = $this->table_data();
        usort( $data, array( &$this, 'sort_data' ) );

        $perPage = 2;
        $currentPage = $this->get_pagenum();
        $totalItems = count($data);

        $this->set_pagination_args( array(
            'total_items' => $totalItems,
            'per_page'    => $perPage
        ) );

        $data = array_slice($data,(($currentPage-1)*$perPage),$perPage);

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

    /**
     * Override the parent columns method. Defines the columns to use in your listing table
     *
     * @return Array
     */
    public function get_columns()
    {
        $columns = array(
            'post_title'                 => 'Title',
            'post_date'                  => 'Date',
            'clicktripz_object_location' => 'Location'
        );

        return $columns;
    }

    /**
     * Define which columns are hidden
     *
     * @return Array
     */
    public function get_hidden_columns()
    {
        return array();
    }

    /**
     * Define the sortable columns
     *
     * @return Array
     */
    public function get_sortable_columns()
    {
        $sortable = array(
            'post_title' => array('post_title', false),
            'post_date'  => array('post_date', false),
        );

        return $sortable;
    }

    /**
     * Get the table data
     *
     * @return Array
     */
    private function table_data()
    {
        $post_data = get_posts( array(
            numberposts => -1
        ));

        $page_data = get_pages();

        $data = array_merge( $post_data, $page_data );

        return $data;
    }

    /**
     * Define what data to show on each column of the table
     *
     * @param  Array $item        Data
     * @param  String $column_name - Current column name
     *
     * @return Mixed
     */
    public function column_default( $item, $column_name )
    {
        switch( $column_name ) {
            case 'post_title':
            case 'post_date':
                return $item->$column_name;
            case 'clicktripz_object_location':
                return '<input type="text" value="'.$item->$column_name.'">';

            default:
                return print_r( $item, true ) ;
        }
    }

    /**
     * Allows you to sort the data by the variables set in the $_GET
     *
     * @return Mixed
     */
    private function sort_data( $a, $b )
    {
        // Set defaults
        $orderby = 'post_title';
        $order = 'asc';

        // If orderby is set, use this as the sort column
        if(!empty($_GET['orderby']))
        {
            $orderby = $_GET['orderby'];
        }

        // If order is set use this as the order
        if(!empty($_GET['order']))
        {
            $order = $_GET['order'];
        }


        $result = strcmp( $a->$orderby, $b->$orderby );

        if($order === 'asc')
        {
            return $result;
        }

        return -$result;
    }
}

As you can see I've managed to put input boxes to custom WP_List_Table I've created.

Custom WP_List_Table

But I couldn't find to way a put a save button to the right lower corner of the page. Also how can I create a save function to save all the changes at once?

Thanks in advance.

1 Answer 1

0

After research I've found a way to get POST data from a form. Thanks to this article, I've successfully managed to get the data from the my WP_List_Table through admin_post mechanism.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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