0

I'm sideloading an image into wordpress from youtube. it works fine and the key part of the code is:

// sideload it into wordpress (generates various sizes)
$thumbid = media_handle_sideload( $file_array, $post->ID );
if ( is_wp_error($thumbid) ) { // deal with error here }

// kill existing attachment first, if any
wp_delete_attachment(get_post_thumbnail_id($post->ID), true);

// and finally associate post with new thumbnail
set_post_thumbnail( $post, $thumbid );

Here's the problem. I have registered 5 image sizes with add_image_size in my Wordpress functions file. When I sideload per the code above, WP generates all the thumbs for the image in all the sizes except one, 640x400. I believe it's skipping this thumb since 640x400 is actually larger than the image that is returned from YouTube. However, I need to get that thumbnail no matter what-- I want to force Wordpress to generate that thumbnail. How can I do that?

I register the image size just like all others:

 add_image_size( 'static-head', 640, 400, true );

So I'm looking for function sort of like wp_generate_thumbnail($thumbid, 'static-head'), that will leave the right 640x400 image in the uploads folder.

Thank you--

0

You'll have to use wp_generate_attachment_metadata and wp_update_attachment_metadata to do this.

// sideload it into wordpress (generates various sizes)
$thumbid = media_handle_sideload( $file_array, $post->ID );
if ( is_wp_error($thumbid) ) { // deal with error here }

// If this function is undefined in the environment where it is to be used,
// such as within a Shortcode, use the include function:
include( ABSPATH . 'wp-admin/includes/image.php' );

// create the thumbnails
$attach_data = wp_generate_attachment_metadata( $thumbid, $file_array['tmp_name'] );
wp_update_attachment_metadata( $thumbid,  $attach_data );

// kill existing attachment first, if any
wp_delete_attachment(get_post_thumbnail_id($post->ID), true);

// and finally associate post with new thumbnail
set_post_thumbnail( $post, $thumbid );

wp_generate_attachment_metadata creates the thumbnails. wp_update_attachment_metadata makes sure they are actually part of the media object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.