I have constructed a WP_List_Table
that displays on a custom theme page that I have built. I am having some trouble getting the row actions to display, despite me reviewing all the possible info on the Internet about WP_List_Table.
Any help would be great!
class Testimonials_List_Table extends WP_List_Table {
function __construct() {
parent::__construct( array(
'singular'=> 'testimonial', //Singular label
'plural' => 'testimonials', //plural label, also this well be one of the table css class
'ajax' => false //We won't support Ajax for this table
) );
}
function get_bulk_actions() {
$actions = array(
'delete' => 'Delete'
);
return $actions;
}
function process_bulk_action() {
if( 'delete'===$this->current_action() ) {
wp_die('Items deleted (or they would be if we had items to delete)!');
}
}
function get_columns() {
return $columns= array(
'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
'name'=> 'Name',
'company_name'=> 'Company Name',
'company_url'=> 'Website URL',
'testimonials_quote'=> 'Testimonial'
);
}
function column_name($item){
$actions = array(
'edit' => sprintf('<a href="?page=%s&action=%s&testimonial=%s">Edit</a>',$_REQUEST['page'],'edit',$item['ID']),
'delete' => sprintf('<a href="?page=%s&action=%s&testimonial=%s">Delete</a>',$_REQUEST['page'],'delete',$item['ID']),
);
//Return the title contents
return sprintf('%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
/*$1%s*/ $item['name'],
/*$2%s*/ $item['ID'],
/*$3%s*/ $this->row_actions($actions)
);
}
function column_cb($item){
return sprintf(
'<input type="checkbox" name="%1$s[]" value="%2$s" />',
/*$1%s*/ $this->_args['singular'], //Let's simply repurpose the table's singular label ("movie")
/*$2%s*/ $item['ID'] //The value of the checkbox should be the record's id
);
}
function prepare_items() {
global $wpdb, $_wp_column_headers;
$screen = get_current_screen();
$wpdb->show_errors();
$query = "SELECT * FROM wp_testimonials";
$orderby = !empty($_GET["orderby"]) ? mysql_real_escape_string($_GET["orderby"]) : 'ASC';
$order = !empty($_GET["order"]) ? mysql_real_escape_string($_GET["order"]) : '';
if(!empty($orderby) & !empty($order)){ $query.=' ORDER BY '.$orderby.' '.$order; }
$totalitems = $wpdb->query($query); //return the total number of affected rows
$perpage = 8;
$paged = !empty($_GET["paged"]) ? mysql_real_escape_string($_GET["paged"]) : '';
if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }
$totalpages = ceil($totalitems/$perpage);
if(!empty($paged) && !empty($perpage)){
$offset=($paged-1)*$perpage;
$query.=' LIMIT '.(int)$offset.','.(int)$perpage;
}
$this->set_pagination_args( array(
"total_items" => $totalitems,
"total_pages" => $totalpages,
"per_page" => $perpage,
) );
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
$this->items = $wpdb->get_results($query);
}
function display_rows() {
$records = $this->items;
list( $columns, $hidden ) = $this->get_column_info();
if(!empty($records)){foreach($records as $rec){
echo '<tr id="record_'.$rec->name.'">';
foreach ( $columns as $column_name => $column_display_name ) {
$class = "class='$column_name column-$column_name'";
$style = "";
if ( in_array( $column_name, $hidden ) ) $style = ' style="display:none;"';
$attributes = $class . $style;
$editlink = '/test/wp-admin/themes.php?page=testimonials&tab=add_new_testimonial='.urlencode((int)$rec->id);
switch ( $column_name ) {
case "cb": echo '<td '.$attributes.'><input type="checkbox" /></td>';break;
case "id": echo '<td '.$attributes.'>'.stripslashes($rec->id).'</td>';break;
case "name": echo '<td '.$attributes.'><strong><a href="'.$editlink.'" title="Edit">'.stripslashes($rec->name).'</a></strong></td>'; break;
case "company_name": echo '<td '.$attributes.'>'.stripslashes($rec->company_name).'</td>'; break;
case "company_url": echo '<td '.$attributes.'>'.stripslashes($rec->company_url).'</td>'; break;
case "testimonials_quote": echo '<td '.$attributes.'>'.stripslashes($rec->testimonials_quote).'</td>'; break;
}
}
echo'</tr>';
}}
}
} // End WP_List_Table class
In
display_rows()
you are using the following code to print the ‘name’ column:Nowhere do you call
column_name()
, which is the function you’ve defined to display the item’s and row actions:See, for example, how the underlying
WP_List_Table
does it.In fact, if you avoid over-riding the underlying class’s
display_rows()
method then it will automatically use yourcolumn_{$column_id}()
method (where it exists), to define the content of the column. This makes it much easier to maintain than your current switch statement.