Skip to content

Instantly share code, notes, and snippets.

Created May 21, 2015 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2fbf94727533c768109f to your computer and use it in GitHub Desktop.
Save anonymous/2fbf94727533c768109f to your computer and use it in GitHub Desktop.
wp_list_table (not working)
//Our class extends the WP_List_Table class, so we need to make sure that it's there
if(!class_exists('WP_List_Table')){
require_once(ABSPATH .'wp-admin/includes/class-wp-list-table.php');
}
//The WP List table class
class bsp_list_table extends WP_List_Table {
/**
* Constructor, we override the parent to pass our own arguments
* We usually focus on three parameters: singular and plural labels, as well as whether the class supports AJAX.
*/
function __construct(){
parent:: __construct(array(
'singular'=>"student",
'plural'=>"students",
'ajax'=>false
));
}
public function prepare_items(){
global $wpdb, $_wp_column_headers;
// $screen=get_current_screen();
$tablename=$wpdb->prefix."students"; //geting our table name with prefix
//selecting every thing from table
$data="SELECT * FROM $tablename";
$this->process_bulk_action();
//if no order, default to asc
$orderby = !empty($_GET['orderby']) ? mysql_real_escape_string($_GET['orderby']) : 'ASC';
//if no order leave it blank (as it is) - we can order it by name or id or...
$order = !empty($_GET['order']) ? mysql_real_escape_string($_GET['order']) : '';
//if it's not empty then query it from databse
if(!empty($orderby) & !empty($order)){ $data.=' ORDER BY '.$orderby.' '.$order; }
/* -- Pagination parameters -- */
//Number of elements in your table?
$totalitems = $wpdb->query($data); //return the total number of affected rows
//How many to display per page?
$perpage = 5;
//Which page is this?
$paged = !empty($_GET['paged']) ? mysql_real_escape_string($_GET['paged']) : '';
//Page Number
if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }
//How many pages do we have in total?
$totalpages = ceil($totalitems/$perpage);
//adjust the query to take pagination into account
if(!empty($paged) && !empty($perpage)){
$offset=($paged-1)*$perpage;
$data.=' LIMIT '.(int)$offset.','.(int)$perpage;
}
/* -- Register the pagination -- */
$this->set_pagination_args( array(
"total_items" => $totalitems,
"total_pages" => $totalpages,
"per_page" => $perpage,
) );
//register the columns
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array( $columns, $hidden, $sortable );
//fetch the items
$this->items=$wpdb->get_results($data,'ARRAY_A');
}
function get_columns(){
$columns=array(
'cb'=>'<input type="checkbox" />',
'students_name'=>__('Name'),
'students_lastname'=>__('Lastname'),
'students_email'=>__('E-mail')
);
return $columns;
}
function get_sortable_columns(){
$sortable=array(
'students_name'=>array('students_name', false),
'students_lastname'=>array('students_lastname', false)
);
return $sortable;
}
function column_default($item, $column_name){
switch($column_name){
//case 'students_id':
case 'students_name':
case 'students_lastname':
case 'students_email':
return $item[$column_name];
default:
//for debugging purposes we print out the whole array
return print_r($item, true);
}
}
function column_name($item){
$delete_nonce = wp_create_nonce( 'bsp_delete_student' );
$title = '<strong>' . $item['students_name'] . '</strong>';
$actions=array(
'delete' => sprintf( '<a href="?post_type%s&page=%s&action=%s&student=%s&_wpnonce=%s">Delete</a>', esc_attr( $_REQUEST['page'] ), $_REQUEST['post_type'], 'delete', absint( $item['students_id'] ) )
//'delete' => sprintf( '<a href="?page=%s&action=%s&student=%s&_wpnonce=%s">Delete</a>', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['students_id'] ), $delete_nonce )
);
return $title . $this->row_actions( $actions );
/*//Return the title contents
return sprintf('%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
/*$1%s/ $item['students_name'],
/*$2%s/ $item['students_id'],
/*$3%s/ $this->row_actions($actions)
);*/
}
function get_bulk_actions(){
$actions=array(
'delete'=> __( 'Delete' )
);
return $actions;
}
public function process_bulk_action() {
//Detect when a bulk action is being triggered
if ('delete' === $this->current_action() ) {
// In our file that handles the request, verify the nonce.
$nonce = esc_attr( $_REQUEST['_wpnonce'] );
if ( !wp_verify_nonce( $nonce, 'bsp_delete_student' ) ) {
die( 'Go get a life script kiddies' );
}
else {
self::delete_student( absint( $_GET['student'] ) );
wp_redirect( esc_url( add_query_arg() ) );
exit;
}
}
// If the delete bulk action is triggered
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'delete' )
|| ( isset( $_POST['action2'] ) && $_POST['action2'] == 'delete' )
) {
$delete_ids = esc_sql( $_POST['delete'] );
// loop over the array of record IDs and delete them
foreach ($delete_ids as $id ) {
self::delete_student( $id );
}
wp_redirect( esc_url( add_query_arg() ) );
exit;
//wp_die('Items deleted (or they would be if we had items to delete)!');
}
}
//function for deleting a student from the table
public static function delete_student($id){
global $wpdb;
$wpdb->delete("{$wpdb->prefix}students",
[ 'students_id' => $id ],
[ '%d' ]
);
}
//ading the checkboxes to the table %1$s we use the tables singular label, %2$s the value of the chechbox is the students id
function column_cb( $item ) {
return sprintf('<input type="checkbox" name="%1$s[]" value="%2$s" />',
$this->_args['singular'],
$item['students_id']
);
}
function no_items() {
_e( 'The database is empty.' );
}
}//end of class
//function for displaying the custom table data in wp_list_table
function bsp_students_show(){
//new instance of wp_list_table
$bsplisttable = new bsp_list_table();
//outputing the headline before table
echo '<div class="wrap"><h2>'. __('Enroled students').'</h2>';
//calling function prepare_items
$bsplisttable->prepare_items();
?>
<!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions -->
<form method="post">
<!-- For plugins, we also need to ensure that the form posts back to our current page -->
<input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
<!-- Now we can render the completed list table -->
<?php
$bsplisttable->display();
?>
</form></div>
<?php
}//end of function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment