In this article, we’ll learn How to Create Custom Image Sizes in WordPress.
Table of Contents
In the visually driven world of the web, captivating imagery plays a crucial role in capturing the attention of your visitors. WordPress, with its versatile framework, empowers you to take control of your website’s aesthetics.
While default image sizes have their place, the ability to create custom image sizes opens up a realm of creative possibilities.
We will dive deeper into the concept of custom image sizes in WordPress and explore innovative ways to leverage this feature to enhance your website’s visual storytelling.
Custom Image Sizes in WordPress
WordPress comes with a number of pre-defined image sizes, but you can also create your own custom sizes to fit your specific needs. This can be useful for creating different-sized images for different purposes, such as:
- Featured images
- Thumbnails
- Post slider images
- Product images
- Portfolio images
By creating custom image sizes, you can tailor the visual experience to suit your specific needs.
Whether it’s a hero image that spans the width of the screen or a thumbnail that complements a grid layout, custom sizes empower you to align your imagery with your website’s design language.
By optimizing images for different sections and templates, you ensure a cohesive and immersive experience for your visitors.
Registering Additional Custom Image Sizes for your Theme
You will need to start by adding the support of post thumbnails by placing the following code in your theme’s functions.php file:
1 |
add_theme_support( 'post-thumbnails' ); |
Once you enable the support for post thumbnails, you can now use the functionality of registering additional custom image sizes in wordpress with the function add_image_size().
To create a custom image size, you need to use the add_image_size()
function. This function takes three arguments:
- Name: The name of your custom image size. This is what you will use to reference the size in your templates.
- Width: The width of your custom image size.
- Height: The height of your custom image size.
You can also optionally specify a third argument, crop
, which determines whether or not WordPress should crop the image to the specified dimensions. If crop
is set to true
, WordPress will crop the image to the exact dimensions you specified. If crop
is set to false
, WordPress will resize the image to fit the specified dimensions, but it may not be perfectly square.
The usage of add_image_size function is like this: add_image_size( ‘name-of-size’, width, height, crop mode );
Example code can look like this:
1 |
add_image_size( 'sidebar-thumb' , 120, 120, true ); // Hard Crop Mode |
2 |
add_image_size( 'homepage-thumb' , 220, 180 ); // Soft Crop Mode |
3 |
add_image_size( 'singlepost-thumb' , 590, 9999 ); // Unlimited Height Mode |
Now if you notice, we have specified three different sort of image sizes. Each has different modes such as hard crop, soft crop, and unlimited height. So lets cover each example and how you can use them for your benefits.
Displaying additional custom image sizes in WordPress theme
Now that you have added the functionality for the desired image sizes lets take a look at displaying them in your WordPress theme. Open the theme file where you want to display the image and paste the following code:
1 |
<?php the_post_thumbnail( 'your-specified-image-size' ); ?> |
Note: This bit of code must be pasted inside the post loop.
That’s all you really have to do to display the additional custom image sizes in WordPress theme. You probably should wrap it around with the styling that fits your need.
Regenerating Additional Image Sizes in WordPress
If you are not doing this on a brand new site, then you probably will have to regenerate thumbnails.
The way add_image_size() function works is that it only generates the sizes from the point it was added into the theme.
So any post images that were added prior to the inclusion of this function will not have the new size. So what we need to do is regenerate the new image size for older post images.
This is made easy by the plugin called Regenerate Thumbnails. Once you install and activate this plugin,
Enabling Additional Custom Image Sizes in WordPress for your Post Content
Even though you have enabled custom image sizes in wordpress theme, the usage is limited only to your theme which does not makes any sense.
All image sizes are being generated regardless, so why not make it available for the post author to use it within the post content. You can do this by using a plugin called Simple Image Sizes.
Once you install and activate this plugin new options will be added on your Settings » Media page. You will see a list of sizes that you defined in your theme. All you have to do is check the box that says “Show in post insertion”.
Custom image sizes in WordPress offer a realm of creative possibilities to elevate your website’s visual storytelling.
By tailoring image dimensions for different sections, optimizing for mobile devices, enhancing accessibility, maintaining visual consistency, and optimizing performance, you can create a captivating and seamless user experience.
Embrace the power of custom image sizes in wordpress to showcase your content in a visually stunning and user-friendly manner, ensuring that your website leaves a lasting impression on your visitors.
Step beyond the confines of default image sizes and unlock the true potential of visual storytelling in WordPress.