How to Register a Custom Image Size in WordPress (+Example)

1.875 from 5
from 8 user
(Rate this post)
How to Register a Custom Image Size in WordPress (+Example)

Registering a New Image Size helps WordPress developers generate different images when the administrator uploads them on the admin panel. In this tutorial, we'll look at the add_image_size function, responsible for registering a new image size in WordPress.

WordPress has a helpful function for adding a new image size.

Guess what the function name is? add_image_size!

Using the add_image_size function is possible in the functions.php file of your current theme or a custom-made WordPress plugin.

add_image_size function syntax in WordPress

the add_image_size function needs a name, width, height, and optional crop ability.

add_image_size( string $name, int $width, int $height, bool|array $crop = false )

$name must be string, setting a unique name is required for the new image size.

$width and $height are integers, and their names represent what they do.

$crop can be true, false, or an array containing the x and y positions of the crop (Ex: array("left", "top")).

You can use ‘left’, ‘center’, or ‘right’ for x position and ‘top’, ‘center’, or ‘bottom’ for y position.

Example of adding a new image size using add_image_size function

Using the add_image_size function is not challenging for a WordPress developer.

In the below example, I registered three different image sizes with different attributes:

//name: 500-500, with:500px, height:500px, forced crop:
add_image_size("500-500", 500, 500, true);

//name: custom-size, with:300px, height:200px, no crop:
add_image_size("custom-size", 300, 200, false);

//name: custom-cropped-size, with:150px, height:100px, crop from center:
add_image_size("custom-cropped-size", 150, 100, array("center", "center"));

Note: if thumbnails are not generating at all (even for default options), check if GD is enable in your PHP.

To do that, open php.ini file in the server and search for extension=gd in the file. If you see ; character in the begining of it’s declaration line, remove the ; to enable GD.

Then restart the web server.

How to regenerate older images based on the new registered image size?

Registering a new image size will apply to future image uploads in the admin panel.

You can use the Regenerate Thumbnails plugin to regenerate image sizes for older images.

I used this plugin so many times. It has the option to skip image sizes that are already available in files.

What do you think about "How to Register a Custom Image Size in WordPress (+Example)"?

Your rating submit was successfull.

Your rating submit was not successfull.