Skip to content

Custom Table Columns

Henner Renardi Setyono edited this page Jul 16, 2022 · 2 revisions

Add Column

H::add_column( $post_type, $column_slug, $column_args );

Append one column to the current table.

PARAMETERS

$post_type (string) - In which post table to append this column.

$column_slug (string) - The column's ID.

$column_args (array / optional) - The column's configuration. You can ignore this param if your slug is one of the Reserved slugs. Everything will be automatically populated.

  • name (string) - The shown label.

    Default: capitalized version of the slug.

  • content (function) - Return the column's content. Parameters: ($post, $custom_fields).

    $custom_fields is gained from get_post_custom().

  • sortable (boolean) - Enable sorting, can only be alphabetically or numerically.

    Default: false

  • icon (string) - Replace the label with icon. Use the icon name from dashicons.

    Default: empty

    Edje WordPress - Icon on Column

  • position_before (string) - Where to append the column.

    Default: 'date' (before Published Date)

  • position_after (string) - Where to prepend the column.

    Default: empty

Example 1 - Add One Column

You have 2 ACF Fields called start_date and end_date. You want to print with the format: "10 Apr - 15 Apr 2020".

H::add_column( 'post', 'start-end-date', [
  'name' => 'Start / End Date',
  'position_after' => 'categories',
  'content' => 'column_start_end_date'
] );

function column_start_end_date( $post, $fields ) {
  if( !isset( $fields['start_date'] ) ) {
    return '-';
  }

  $start_date = DateTime::createFromFormat( 'Ymd', $fields['start_date'][0] );
  $end_date = DateTime::createFromFormat( 'Ymd', $fields['end_date'][0] );

  return $start_date->format( 'd M' ) . ' - ' . $end_date->format( 'd M Y' );
}

Reserved Column Slugs

Using one of the reserved slug means the content is automatically populated. They are:

  1. Built-in Columns

    • title - Post's title with edit, quick edit, etc links
    • author
    • thumbnail - Featured image 75x75 size
    • content
    • comments
    • date - Published date
  2. Taxonomy Slug like category.

    • List all terms in comma-separated string.
  3. ACF Custom Field


Override Columns

H::override_columns( $post_type, $columns );

Override all the table columns with your list.

PARAMETER

$post_type (string)

$columns (array) - Collection of column's arguments. Same list as above, but don't use position_before and position_after.

Example 2

In our Product post type, we have price and discount_percent custom field.

We want to have a table with these columns:

  1. Product Thumbnail
  2. Title
  3. Price
  4. Discount % and how much you save
  5. Published Date

Edje WordPress - Complex Column

H::override_columns('product', [
  'thumbnail' => [], // 'thumbnail' is a reserved slug, so no args needed.
  'title' => [], 
  'price' => [],     // 'price' is also reserved since it's the exact name of our ACF Field.
  'discount' => [
    'name' => 'Discount',
    'content' => 'show_discounted_price'
  ]
  'date' => [],
]);

function show_discounted_price( $post, $fields ) { 
  $discount = isset( $fields['discount'] ) ? $fields['discount'][0] : null;
  $price = isset( $fields['price'] ) ? $fields['price'][0] : null;

  $total = $price - ($price * $discount / 100);
  $saving = $price - $total;

  return $discount . '% Discount - You save ' . $saving;
}