0

I am building a custom WordPress plugin where I create a custom list table for entities in a custom database table, extending the WP_List_Table class.

The iteration of the columns for each row happens inside the column_default() function:

public function column_default( $item, $column_name )
{
    global $wpdb;
    switch( $column_name ) {            
        case 'electioncitizen_electionnum':
                $actions = array(
                            'view'      => sprintf('<a href="?page=%s&action=view&electionnum=%s">View</a>', $_REQUEST['page'], $item['electioncitizen_electionnum'])
                             );
                return sprintf('%1$s %2$s', $item[ $column_name ], $this->row_actions($actions) );
        case 'electioncitizen_surname':
        case 'electioncitizen_firstname':
        case 'electioncitizen_fathername':
        case 'electioncitizen_birthdate':
        case 'electioncitizen_cityarea':
            return $item[ $column_name ];
        default:
            return print_r( $item, true ) ;
    }
}

Everything works as expected but I want to style each row based on the value of a specific column of the row that is returned from the DB.

For example:

If the electioncitizen_electionnum column of the current item that is printed on the list table equals 1000 ($item['electioncitizen_electionnum'] == '1000'), the current item's row must have yellow background color.

Is there a way to apply a CSS class to a row based on the values of its columns?

  • to apply a class to a row, you need to add a class attribut to tr. you can do that with a custom method for single_row. It looks like that can be done with custom post type. Are you sure you need a separated database table ? – mmm Oct 20 '17 at 10:21
  • Unfortunately, the custom database tables are needed and there is no option doing it with custom post types for various reasons, backward compatibility included. – L.Milo Oct 20 '17 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.