0

Is there any way to add a custom css class based on post data in the custom post type admin table?

wordpress custom class in admin table row

1 Answer 1

2

Finally got a solution. Founded in WordPress Documentation

add_filter('post_class', 'set_row_post_class', 10,3);
function set_row_post_class($classes, $class, $post_id){
    if (!is_admin()) { //make sure we are in the dashboard 
        return $classes;
    }
    $screen = get_current_screen(); //verify which page we're on
    if ('my-custom-type' != $screen->post_type && 'edit' != $screen->base) {
        return $classes;
    }
    //check if some meta field is set 
    $profile_incomplete = get_post_meta($post_id, 'profile_incomplete', true);
    if ('yes' == $profile_incomplete) {
        $classes[] = 'profile_incomplete'; //add a custom class to highlight this row in the table
    }

    // Return the array
    return $classes;
}
1
  • I think it should be if ('my-custom-type' != $screen->post_type || 'edit' != $screen->base) { (|| instead of &&) Jan 20 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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