Skip to content

Instantly share code, notes, and snippets.

@imvaskii
Last active October 11, 2016 23:12
Show Gist options
  • Save imvaskii/44b8569b6a8934b5fb1ec4124b203672 to your computer and use it in GitHub Desktop.
Save imvaskii/44b8569b6a8934b5fb1ec4124b203672 to your computer and use it in GitHub Desktop.
WordPress admin custom columns.
<?php
/**
* @author : Bhaskar K C
* generic class for admin columns
* Assuming column values are to be stored in post meta
*
* Columns values can be extended with following usage example
* add_action( 'twr_admin_columns', 'callback_function_name', 2, 10 );
* function callback_function_name( $post_type, $column_key ) { // do something here }
*/
if( ! class_exists('TWR_admin_columns') ) {
class TWR_admin_columns {
private $post_type;
private $column_arr = array();
public function __construct( $post_type, $column_array ) {
if( ! is_admin() ) {
return false;
}
if( empty( $post_type ) || empty( $column_array ) ) {
return true;
}
$this->post_type = $post_type;
$this->column_arr = $column_array;
// Competition admin custom columns
add_filter( "manage_{$post_type}_posts_columns", array( &$this, "define_custom_columns" ) );
add_filter( "manage_edit-{$post_type}_sortable_columns", array( &$this, "sortable_admin_columns" ) );
add_action( "manage_{$post_type}_posts_custom_column", array( &$this, 'custom_column_value' ) );
}
function define_custom_columns( $columns ) {
return ( ! empty ( $this->column_arr ) ? $this->column_arr : $columns );
}
function sortable_admin_columns( $sortable_columns ) {
if( empty( $this->column_arr) ) return $sortable_columns;
foreach( $this->column_arr as $sortable_column_key => $column_value ) {
$custom_sortable_columns[$sortable_column_key] = $sortable_column_key;
}
return ( ! empty ( $custom_sortable_columns ) ? $custom_sortable_columns : $sortable_columns );
}
function custom_column_value( $column ) {
if( empty( $this->column_arr) ) return $column;
// adding action to provide flexibility in terms of calculating column values
do_action('twr_admin_columns', $this->post_type, $column );
// Assuming column values are in post meta, so that retrieving them
global $post;
$post_id = $post->ID;
foreach( $this->column_arr as $column_key => $column_title ) {
if( $column != $column_key ) continue;
$custom_sortable_columns[$column_key] = $column_key;
$column_value = get_post_meta( $post_id, $column_key, true );
echo ( ! empty( $column_value )? $column_value: '' );
}
}
}
}
// Usage examples.
// Competition admin custom column array
function get_competition_admin_column_arr() {
return array(
'cb' =>'<input type="checkbox" />',
'title' =>'Title',
'entrants' =>'Entrants',
'start_date' =>'Start time',
'duration' =>'Duration',
'status' =>'Status'
);
}
$competition_columns = new TWR_admin_columns( 'competition', get_competition_admin_column_arr() );
// Extending custom column value example.
add_action( 'twr_admin_columns', 'custom_admin_column_val', 2, 10 );
function custom_admin_column_val( $post_type, $column_key ) {
if( $post_type == 'competition' ) {
switch ( $column_key ) {
case 'status':
echo 'Not active';
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment