Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jerclarke/faa6a9da4b0dfe5c524016b40b67a164 to your computer and use it in GitHub Desktop.
Save jerclarke/faa6a9da4b0dfe5c524016b40b67a164 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);
?>
@joyously
Copy link

The comments say that the code does more than it actually does. This filter is not doing anything, and does not affect the variable being filtered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment