Created
January 16, 2015 16:31
-
-
Save rosswintle/1222708f0d51b4ddfc4d to your computer and use it in GitHub Desktop.
WordPress Custom Columns helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* WordPress Custom Columns helper | |
* | |
* NOTE: The callback function should echo the output. Use global $post to get the post that's | |
* being displayed in the list. | |
* | |
* Example usage: | |
* | |
* require_once('class_oikos_custom_columns.php'); | |
* | |
* OikosCustomColumns::add_column('members', 'country', 'Country', | |
* function() { | |
* global $post; | |
* $member_id = $post->ID; | |
* $country_id = get_post_meta($member_id, '_country_id', true); | |
* $country_name = get_the_title($country_id); | |
* echo $country_name; | |
* } | |
* ); | |
* | |
*/ | |
class OikosCustomColumns { | |
static function add_column($post_type, $column_name, $column_label, $callback) { | |
// Add a filter to add extra columns for a specific post type's edit list | |
add_filter("manage_edit-${post_type}_columns", | |
// This function takes an array of columns and should return an array of columns. It should add | |
// to the list of columns that's given as an input. The array keys are column ID strings and the array | |
// values are column heading strings. | |
function($columns) use ($column_name, $column_label) { | |
$columns[$column_name] = $column_label; | |
return $columns; | |
} | |
); | |
// Add an action to populate the custom columns | |
add_action("manage_${post_type}_posts_custom_column", | |
// This function is called for all columns. It takes a string that's a column ID from the $columns | |
// array mentioned above. The function should determine which column is being output and output the | |
// content of that column for the current post. Note the column content should be OUTPUT, not | |
// returned. | |
function($column) use ($column_name, $callback) { | |
if( $column == $column_name ) { | |
$callback(); | |
} | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment