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

3 from 5
from 2 user
(Rate this post)

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.

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.


	//register [my-post-date] shortcode
	add_shortcode( 'my-post-date', 'my_post_date_callback' );

	define [my-post-date] callback
	-> use date-format to define date format
	-> use post-id to specify a post id
	ex: [my-post-date date-format='F j, Y' post-id=1]
	function my_post_date_callback($attributes) {

		$dateFormat = '';

		$postId = null;

		//check if date-format attribute is present in shortcode
			$dateFormat = $attributes["date-format"];

		//check if post-id attribute is present in shortcode
		//and make sure value is numeric
		if(isset($attributes["post-id"]) && is_numeric($attributes["post-id"]))
			$postId = $attributes["post-id"];

		//do not use echo, just use return
		return get_the_date($dateFormat, $postId);

$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.