26 April 2014

Membuat Plugin Sederhana di Wordpress (5)


Untuk membuat tabel, dapat menggunakan kelas WP_List_Table bawaan dari Wordpress.


if( ! class_exists( 'WP_List_Table' )){
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
// referensi : http://wpengineer.com/2426/wp_list_table-a-step-by-step-guide/
class Sesi_Table extends WP_List_Table{

    function prepare_items()
    {      
         $columns = $this->get_columns();
         $hidden = array();
         $sortable = $this->get_sortable_columns();
         $this->_column_headers = array($columns, $hidden, $sortable);
         $orderby = (empty($_GET['orderby'])) ? 'name': $_GET['orderby'];
         $order = (empty($_GET['order'])) ? 'ASC': $_GET['order'];        
         global $wpdb;
         $this->items = $wpdb->get_results("SELECT id, name, description FROM " . $wpdb->prefix . "gfaq_sesi ORDER BY " . $orderby . ' ' . $order, ARRAY_A);      
    }
  
    /** Comobo box Bulk Actions **/
    function get_bulk_actions() {
        $actions = array(
            'delete'    => 'Delete'
        );
        return $actions;
    }
    /** Buat checkbox di kolom tabel **/
    function column_cb($item){
        return sprintf('', $item['id']);
    }
  
    /** Buat link edit & hapus kolom nama tabel **/
    function column_name($item){
         $actions = array(
            'edit'      => sprintf('Edit',$_REQUEST['page'],$item['id']),
            'delete'    => sprintf('Delete',$_REQUEST['page'],$item['id']),
        );

        return sprintf('%1$s %2$s', $item['name'], $this->row_actions($actions) );
    }
  
    /** Buat kolom di tabel **/
    function get_columns(){
        $columns = array(
            'cb' => '',
            'name' => 'Sesi',
            'description' => 'Deskripsi',
        );
        return $columns;
    }
  
    /** Buat kolom agar bisa diurutkan **/
    function get_sortable_columns() {
        $sortable_columns = array(
            'name' => array('name', false),
            'description' => array('description',false)
        );
        return $sortable_columns;
    }
      
    function column_default( $item, $column_name ) {
        switch( $column_name ) {
            case 'name':
                return $item[ $column_name ];
            case 'description':
                return $item[ $column_name ];
            default:
                return print_r( $item, true ) ;
    }  
}
}


0 komentar:

Posting Komentar