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

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

5 from 5
from 1 user
(Rate this post)

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

Guess what is the function name? 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.

in_category($category, $post = null)

$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 x and y position 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 3 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"));

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

When you register a new image size, It will apply to future image uploads in the admin panel.

For regenerating image sizes for older images, you can use the Regenerate Thumbnails plugin.

I used this plugin so many times. It has an option to skip image sizes that already are 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.

Menu