Skip to content

Instantly share code, notes, and snippets.

@RevConcept
Last active August 28, 2015 06:51
Show Gist options
  • Save RevConcept/7a6236b1bfa0c0317672 to your computer and use it in GitHub Desktop.
Save RevConcept/7a6236b1bfa0c0317672 to your computer and use it in GitHub Desktop.
Adds thumbnail of attachment to admin columns and image preview to CPT admin post page. CPT in example is 'guest-photos'. Using this in addition to "Frontend Uploader" plugin (https://wordpress.org/plugins/frontend-uploader/)
/* ===========================================
Show Guest Photo Attachment in CPT Admin Columns
=============================================*/
// GET ATTACHED IMAGE
function revcon_get_attachment_image($post_ID) {
$images = get_attached_media('image', $post_ID);
if ($images) {
foreach ($images as $image) {
$attachment_id = $image->ID;
$thumbnail_url = wp_get_attachment_image( $attachment_id, array(60,60) );
return $thumbnail_url;
}
}
}
// ADD NEW COLUMN TO ADMIN
function revcon_columns_head($defaults) {
$defaults['attachment_image'] = 'Photo';
return $defaults;
}
// SHOW THE ATTACHMENT IMAGE IN COLUMN
function revcon_columns_content($column_name, $post_ID) {
if ($column_name == 'attachment_image') {
$post_featured_image = revcon_get_attachment_image($post_ID);
if ($post_featured_image) {
echo $post_featured_image;
}
}
}
add_filter('manage_guest-photos_posts_columns', 'revcon_columns_head'); // "manage_guest-photos_posts_columns" will show this ONLY on the CPT guest-photos
add_action('manage_guest-photos_posts_custom_column', 'revcon_columns_content', 10, 2);
/* ===========================================
Show Guest Photo Attachment on CPT Post in Admin
=============================================*/
// GET ATTACHED IMAGE
function revcon_render_attachment_metabox( $post ) {
$output = '';
$images = get_attached_media('image', $post->ID);
if ($images) {
foreach ($images as $image) {
$attachment_id = $image->ID;
$thumbnail_url = wp_get_attachment_image( $attachment_id, 'medium' );
$output = $thumbnail_url;
}
}
echo $output;
}
// ADD METABOX TO CPT
function revcon_add_attachment_thumbs_metabox (){
add_meta_box ('att_thumb_display', 'Attachmed Images','revcon_render_attachment_metabox','guest-photos', 'normal', 'high');
}
add_action( 'add_meta_boxes', 'revcon_add_attachment_thumbs_metabox' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment