wp code snippet

WordPress, PHP quick help codes

Create additional image sizes in WordPress

Step 1: You need to add post thumbnails support by placing below code in functions.php

add_theme_support( 'post-thumbnails' );

Step 2: After enabling post thumbnails support, you are now enabled to register additional images sizes  in WordPress with the function add_image_size().

Uses: add_image_size(‘name_of_size’, width, height, crop_mode);

Example:

add_image_size( 'prefix1-thumb', 120, 120, true ); //Hard Crop
add_image_size( 'prefix2-thumb', 220, 180 ); //Soft Crop

Hard Crop: This mode will automatically crop the image either from the sides or from the top and bottom depending on the size.

Soft Crop Mode: This mode will resizes the image proportionally without distorting it. So you might not get the dimensions that you wanted.

Step 3: Add custom image sizes to Media Uploader.

Example:

function show_image_sizes_callback($sizes) {
$addsizes = array(
"prefix1-thumb" => __( "Prefix1 Name"),
"prefix2-thumb" => __( "Prefix2 Name")
);
$newsizes = array_merge($sizes, $addsizes);
return $newsizes;
}
add_filter('image_size_names_choose', 'show_image_sizes_callback');

Thats all!

Categories: WordPress

Leave a Reply

%d bloggers like this: