Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ofernandolopes/84158b1b891d5cd7b8645f2f045b473f to your computer and use it in GitHub Desktop.
Save ofernandolopes/84158b1b891d5cd7b8645f2f045b473f to your computer and use it in GitHub Desktop.
Demo using WP's admin_post_thumbnail_html filter to insert content in featured image metabox
<?php
/**
* Adds a detailed UI to WP's thumbnail HTML markup used in the Feature Image metabox.
*
* Adds a description of featured images, a listing of the formats used by GV and
* previews of how the images will appear in features slider and thumbnails.
*
* Note that $thumbnail_id is only saved when the post is saved. Immediately after the featured image is changed
* this value is updated, but not the postmeta field in the database (nor OUR fields which are tied to it)
* SO:
* - empty $thumbnail_id means EITHER the post didn't have a thumbnail when it was saved OR that the "remove featured image" button was just pressed.
* - non-empty $thumbnail_id means EITHER the post was saved with a featured image OR an unsaved featured image was added and this is the preview.
*
* @param string $output The thumbnail HTML markup
* @param int $post_id The post ID of the post being edited
* @param int $post_id The post ID for the attachment post
*/
function gv_filter_admin_post_thumbnail_html($output, $post_id, $thumbnail_id = ""){
// ...
/**
* If this post has a featured image set
* Note: Could be current (saved in DB) value, or temporary (used for generating preview) thumbnail_id
*/
if ($thumbnail_id) :
/**
* Get the post id of the attachment that was chosen as featured image
*/
$featured_image_post_id = $thumbnail_id;
/**
* Get the data arrays ['url', 'width', 'height', 'is_intermediate' ] about our CUSTOM sizes
* Note: WP will return the largest size available even if it's too small, so these dimensions
* are likely to describe "featured_image_small" (CUSTOM) or "full size" (original upload).
*/
$featured_image_large_data = wp_get_attachment_image_src($featured_image_post_id, 'featured_image_large');
-
- // For the record: No stated size string gives you the old small square 'thumbnail' size
- //$featured_image_thumbnail_data = wp_get_attachment_image_src($featured_image_post_id);
/**
* Determine the width and height of the file chosen for featured_image_large
*/
$featured_image_large_width = $featured_image_large_data[1];
$featured_image_large_height = $featured_image_large_data[2];
// ...
endif;
return $output;
}
add_filter('admin_post_thumbnail_html', 'gv_filter_admin_post_thumbnail_html', 10, 3);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment