[wp-hackers] add_image_size() possible to restrict to just one size?..

Patrik Bóna patrik.bona at mrhead.sk
Sun Sep 5 10:44:47 UTC 2010


Hi Davit,

You can use wp_generate_attachment_metadata filter. I use it for 
generating own image sizes for different post types. This is example for 
post type galeria (gallery):

function gallery_upload($metadata, $attachment_id) {
	$post_id = $_POST['post_id'];

	// if it is not 'galeria' just return metadata
	if(get_post_type($post_id) != 'galeria')
		return $metadata;

	// otherwise create custom image sizes
	$upload_dir = wp_upload_dir();
	$file = image_resize($upload_dir['basedir'] . '/' . $metadata['file'], 100, 100, true, 'thumb');
	$metadata['sizes']['thumbnail'] = array('file' =>  basename($file), 'width' =>  100, 'height' =>  100);
	return $metadata;
}
add_filter('wp_generate_attachment_metadata', 'gallery_upload', 10, 2);

So i don't use add_image_size (in this case) because it apply to 
everything. Disadvantage of this solution is that these sizes are not 
deleted automatically when you delete attachment, but you can again use 
filters/actions... ;)

Patrik

On 09/05/2010 12:17 PM, Davit Barbakadze wrote:
> In my understanding now, whenever I add new size, wordpress just
> automatically creates it on every upload. That is if I got five
> different size, for post thumbnails, for sidebar widgets, avatars,
> custom category headers, etc - they all get created on every upload.
> Is it so? If for example if I upload an image for an avatar, do the
> post thumbnail size, and sidebar widget size and category header size
> get created too? If yes, can I prevent it, and restrict maybe to only
> an avatar size for that particular case?
>
>    



More information about the wp-hackers mailing list