0

I want to change the background-color of each row in the posts table based on some date checks.

The css I need to change is tr#post-nnnn {background-color:$myColor} where nnnn is the post number and $myColor is an array of 5 predefined colors.

I have had a think about how to go about this and come up with the following as an outline, but would appreciate some advice on whether I'm heading in the right direction and what should be the standard process for solving this kind of problem.

Here's the general pseudo-idea...

add_action( 'admin_print_styles-edit.php', 'my_order_highlighter' );  // After some searching I decided this would be the place to attach my function...

function my_order_highlighter() {

$order_n = get Order number
$date_o = get order date
$date_r = get requested date
$date_t = get todays date
$order_status = get order status (shipped/processing)
h_color = 1:red, 2:pink, 3:orange, 4:light-orange, 5:green

For each order displayed on the page…


If ($order_status  == 'shipped') {
    //  style the background colour for the current order row as '5' - "Completed"
    <style> tr.$order_n {background-color:#get_h_color(5)}</style> 
    }
    // order is NOT shipped so check if there is a 'requested date' entered…
    if ($date_r is set) {   
        // If due date is less than or equal to 3days away…         
        If ($date_t - $date_r <= 3days) {    
            // style the background colour for the current order row as '3' - "Needs attention"
            <style> tr.$order_n {background-color:get_h_color(3)}</style> 
        }
        // Due date is more than 3days away so style row as '4' "Upcoming"
        <style> tr.$order_n {background-color:#get_h_color(4)}</style> 
    }
    // No requested date entered so check if it is 5 days or more since order…
    If ($date_t - $date_o >= 5days) {
    // style the background colour for the current order row as '1' - "Needs URGENT attention"
    <style> tr.$order_n {background-color:#get_h_color(1)}</style> 
    }

    //otherwise, style row as '4' "Upcoming"
    <style> tr.$order_n {background-color:#get_h_color(4)}</style> 
}

Is this a workable idea or am I looking in the wrong place to accomplish this?

0

Your code is badly broken and it is going to be an awkward process to hack the styles in like you are trying to do. It looks like you are using some third part code but assuming that that code uses WP_List_Table as it probably should you can alter the CSS of the rows as they are generated:

function alter_list_table_css_wpse_192010($classes) {
  if (is_admin() && in_array('iedit',$classes)) {
    // define your colors
    $colors = array(
      'red', 'pink', 'orange', 'light-orange', 'green'
    );
    // pull in the post data
    global $post;
    // var_dump($classes,$post->post_date);
    // do your date math
    // and add your classes
    $classes[] = 'date-bg-'.$colors[0];
  }
  return $classes;
}
add_action(
  'post_class',
  'alter_list_table_css_wpse_192010',
  10,3
);

Then, all you have to do is add CSS for the five different backgrounds, but you know what the classes are so there is no tricky logic involved.

Caveats:

  1. I don't know quite what your date math is supposed to look like so I didn't try to write that part.
  2. I don't know exactly what date you are trying to use. It looks to be non-Core.
  3. I grepped my install and the iedit class appears only once in Core in the list table so I am using it to identify our presence in that table. It is a bit of a hack.
3
  • Thanks for you response s_ha_dum. My code was purely to demonstrate how I was thinking it might be achieved. The purpose of this idea is to highlight orders in my woocommerce shop that are either approaching or have passed 7 days since the order was placed, with colours signifying varying degrees of urgency. I really want to learn how to figure this stuff out for myself but I'm not finding any useful sources to help understand the process. So the real question I guess is how did you KNOW that WP_List_Table should be the place to start? – WPD_tobe Jun 24 '15 at 14:43
  • I know that the codex is filled with info on how to work with filters and actions etc. but you need to KNOW exactly what you are looking for in order to find it...? Some direction would be greatly appreciated. – WPD_tobe Jun 24 '15 at 14:44
  • @WPD_tode : an indispensable tool for finding filters and a lot of other things-- grep. – s_ha_dum Jun 24 '15 at 14:57

Your Answer

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

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