Recipe: add a column to a list table
Goal: a new column in an admin list, for example the product list, that works with Addofy's list styling.
Use the native hooks
Addofy supports the native WordPress hooks for columns. Add yours the usual way:
add_filter( 'manage_edit-product_columns', static function ( array $columns ) {
$columns['myplugin_margin'] = esc_html__( 'Margin', 'myplugin' );
return $columns;
} );
add_action( 'manage_product_posts_custom_column', static function ( string $column, int $post_id ) {
if ( 'myplugin_margin' === $column ) {
echo esc_html( myplugin_margin( $post_id ) );
}
}, 10, 2 );
adba_list_table_columns runs after every plugin added theirs and receives the final column key => label list and the screen id. Use it only to change what the other plugins did.
Show it on tablets
On a tablet (601 to 782px) Addofy decides per column whether it stays visible. Give your column a priority with adba_column_priority:
- 1 always shows.
- 2 shows from 701px.
- 3 does not show on a tablet. A column without a priority counts as 3.
add_filter( 'adba_column_priority', static function ( array $priorities, string $screen_id ) {
if ( 'edit-product' === $screen_id ) {
$priorities['edit-product']['myplugin_margin'] = 2;
}
return $priorities;
}, 10, 2 );
Order and width
Users can drag columns into their own order and, with the experimental Column widths module, change widths. You do not need to do anything for that. To set an order or width in code, see adba_column_order and adba_column_widths.