Generates metadata for the image attachment and creates intermediate copies of the image - thumbnails of all registered sizes.
Intermediate sizes are specified in "Media Settings" (thumbnail, medium, large), and they can also be created by the function add_image_size() in the theme or plugin.
The function does not update the attachment metadata, but only creates them and all the necessary image sizes. Usually, after the function runs, you need to update the metadata using wp_update_attachment_metadata().
The function updates the meta-field of the post _thumbnail_id associated with the post's thumbnail.
This function can be used to recreate the thumbnail of the image attachment and all its intermediate sizes after the sizes have been changed in "Media Settings". However, this function does not delete already created intermediate sizes, but creates new ones in addition to the existing ones.
The thumbnail and intermediate sizes of the image, and the array element ["sizes"] are returned by the function only when the created size is smaller than the original size of the image; in other cases, the intermediate size is simply not created.
The function is not defined on the front-end, so to use it, for example, in a shortcode, you need to include it by adding the following line to the code:
Let's say we have an attachment with ID 75 and the picture has already been uploaded and is in the uploads folder. Now let's create all its intermediate sizes:
#2 Add already downloaded file to WP Media directory and to DB as attachment
Working example, shows how you can upload a picture to the uploads folder, attach it to the post 37 and create a thumbnail and all the intermediate dimensions of that picture.
// the file must be in the WP uploads (media) directory.
$filename = '/path/to/uploads/2013/03/filname.jpg';
// the ID of the post to which we are attaching the attachment.
$parent_post_id = 37;
// Check the post type that we will use in the 'post_mime_type' field.
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the download directory.
$wp_upload_dir = wp_upload_dir();
// Prepare the array with the necessary data for the nesting.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the post into the database.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
// Connect the desired file, if it is not already connected
// wp_generate_attachment_metadata() depends on this file.
require_once ABSPATH . 'wp-admin/includes/image.php';
// Create metadata for the attachment and update the post in the database.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
Changelog
Since 2.1.0
Introduced.
Since 6.0.0
The $filesize value was added to the returned array.