Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
share|improve this question

1 Answer 1

up vote 3 down vote accepted

In display_rows() you are using the following code to print the 'name' column:

echo '<td '.$attributes.'><strong><a href="'.$editlink.'" title="Edit">'.stripslashes($rec->name).'</a></strong></td>';

Nowhere do you call column_name(), which is the function you've defined to display the item's and row actions:

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

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 your column_{$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.

share|improve this answer
    
Great! I like your idea of not over-riding display_rows(). What exact code do I need to remove to see an example of this in action? Thanks for your help! –  Rob Myrick Dec 18 '12 at 18:10
    
@RobMyrick simply remove display_rows() from the code you have. Then define column_foo() to define the 'foo' column for a particular row. (e.g. column_company_name()). It is passed the item as an argument. –  Stephen Harris Dec 18 '12 at 19:20
    
You rock....thanks man. –  Rob Myrick Dec 18 '12 at 19:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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