Skip to content

Instantly share code, notes, and snippets.

@szbl
Last active December 15, 2015 07:49
Show Gist options
  • Save szbl/5225903 to your computer and use it in GitHub Desktop.
Save szbl/5225903 to your computer and use it in GitHub Desktop.
How to add custom admin columns in WordPress for a sample post type "szbl-location" – this is from my WordCamp San Diego 2013 presentation. This is a bit simplified.
<?php
/*
Add the actual columns to the szbl-location edit listing admin screen.
*/
add_filter( 'manage_edit-szbl-location_columns', 'szbl_manage_edit_columns', 10 );
function szbl_manage_edit_columns( $cols )
{
$new_cols = array(
'cb' => '<input type="checkbox">',
'title' => __( 'Location Name' ),
'szbl-location-address' => __( 'Address' ),
'szbl-location-phone' => __( 'Phone' ),
'szbl-location-img' => __( 'Image' )
);
return $new_cols;
}
/*
Output custom column content.
*/
add_action( 'manage_szbl-location_posts_custom_column', 'szbl_manage_posts_column', 10 );
function szbl_manage_posts_column( $column_name )
{
$post_id = get_the_ID();
switch ( $column_name )
{
case 'szbl-location-address':
$address get_post_meta( $post_id, 'szbl_location_address', true );
echo esc_html( $address );
break;
case 'szbl-location-phone':
$phone = get_post_meta( $post_id, 'szbl_location_phone', true );
echo esc_html( $phone );
break;
case 'szbl-location-img':
if ( has_post_thumbnail() )
{
echo '<a href="' . get_edit_post_link( $post_id ) . '">';
the_post_thumbnail( 'thumbnail' );
echo '</a>';
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment