Skip to content

manage_posts_columns

Mark Howells-Mead edited this page Aug 30, 2022 · 3 revisions

For use within the Hello FSE Package structure.

public function run(){
	add_filter("manage_{$this->post_type}_posts_columns", [$this, 'adminColumns']);
	add_action("manage_{$this->post_type}_posts_custom_column", [$this, 'adminColumnContent'], 10, 2);
}

// …

/**
 * Add custom admin columns at position 2 in the columns array
 *
 * @param array $columns
 * @return array
 */
public function adminColumns($columns)
{
	$insert_at = 2;
	$columns = array_merge(
		array_slice($columns, 0, $insert_at, true),
		['message' => _x('Message', 'Admin column title', 'shp_wc_order_sms')],
		['order_id' => _x('Order ID', 'Admin column title', 'shp_wc_order_sms')],
		['sent_time' => _x('Last sent', 'Admin column title', 'shp_wc_order_sms')],
		array_slice($columns, $insert_at, null, true)
	);
	return $columns;
}

/**
 * Display custom admin column content
 *
 * @param string $column
 * @param integer $post_id
 * @return void
 */
public function adminColumnContent($column, $post_id)
{
	switch ($column) {
		case 'order_id':
			$order_id = get_post_meta($post_id, 'order_id', true);
			if (!(int) $order_id) {
				echo '-';
			} else {
				echo (int) $order_id;
			}
			break;
		case 'message':
			$message = get_post_meta($post_id, 'message', true);
			if (!empty($message)) {
				echo sanitize_textarea_field($message);
			} else {
				echo '-';
			}
			break;
		case 'sent_time':
			$sent = get_post_meta($post_id, 'sent', true);
			if (!empty($sent)) {
				echo wp_date('d.m.Y H:i:s', strtotime($sent));
			} else {
				echo '-';
			}
			break;
	}
}
Clone this wiki locally