wordpress register image size

In this WordPress tutorial we will learn how to register a new image size in WordPress.

Why we need a new image size

WordPress has few default sizes for images, like thumbnail, medium, large, full. When you upload an image, it will automatically create additional images to these sizes.

You can create additional image size as per your need. Like you may need new image size to use as featured image on homepage.

Follow below steps to create a new image size and use it as featured image.

Step 1: Enable Image size function

Copy paste below code in functions.php file of your wordpress theme to enable add_image_size() function. If this function is already in functions.php file, you can skip this step.


add_theme_support( 'post-thumbnails' );

Step 2: Add New Image Size

In this step we will use add_image_size() function to create a new image size.

Syntax


add_image_size( 'image-size-name', width, height );

Example:
Let's say we want to create a new image size of name homepage-thumbnail of width 300px and height 200px. Add below code for this in functions.php file of your theme.


add_image_size( 'homepage-thumbnail', 300, 200 );

Step 3: Use new image size

Now if we want to use this new size image homepage-thumbnail as featured image, use below code.


<?php if ( has_post_thumbnail() ) {
	the_post_thumbnail('homepage-thumbnail');
} ?>

Regenerate images of new size for previously uploaded images

You should now generate images of new size for previously uploaded images. You can do this easily by just few clicks using Regenerate Thumbnails plugin.

Install the plugin and navigate to:
Dashboard >> Tools >> Regenerate Thumbnails

Here, you can generate images of new size by just a click. I hope this tutorial was helpful to you.

Tags