Register New Image Size in WordPress

wordpress-new-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.

1
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

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

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

1
2
3
<?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.
WordPress-Regenerate-Thumbnails-Plugin

I hope this tutorial was helpful to you.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *