Skip to main content
Login Join
Snippet · PHP

Regenerate Missing Image Sizes

Shared by Darshit Rajyaguru · May 14, 2026 · @add_attachment

28 views
Back to Snippets

Generates missing thumbnails automatically when images are uploaded.

add_action( 'add_attachment', function( $attachment_id ) {
	$file = get_attached_file( $attachment_id );
	if ( ! $file || ! wp_attachment_is_image( $attachment_id ) ) {
		return;
	}
	require_once ABSPATH . 'wp-admin/includes/image.php';
	$metadata = wp_generate_attachment_metadata( $attachment_id, $file );

	if ( ! is_wp_error( $metadata ) ) {
		wp_update_attachment_metadata( $attachment_id, $metadata );
	}
} );
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments