How to Add Additional Image Sizes in WordPress

Home » Snippets » How to Add Additional Image Sizes in WordPress

Created with:



Creator: Alex

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project

Images are a vital part of any website, and WordPress makes it incredibly easy to add and manage them. With several built-in image sizes to choose from, you can display your images in a variety of ways. However, what happens when the default sizes don’t quite fit your needs? This is where adding additional image sizes comes in. Whether you want to create a custom size for a specific part of your site or simply need more flexibility with your images, this article will show you how to add additional image sizes to your WordPress site.

					function wpturbo_add_image_sizes() {
    add_image_size( 'wpturbo_custom_size', 800, 600, true ); // add custom image size
    add_image_size( 'wpturbo_featured_size', 1200, 800, true ); // add featured image size
add_action( 'after_setup_theme', 'wpturbo_add_image_sizes' );

The code snippet above shows us how to add additional image sizes to a WordPress site. This can be very helpful if we want to use images in different sizes and dimensions throughout our website, like a custom image size for a specific section or a featured image size for our blog posts.

This code works by adding a new function wpturbo_add_image_sizes() that uses the add_image_size() function to define our custom image sizes.

The add_image_size() function takes four arguments:

  1. The first argument is the named value for the custom image size; wpturbo_custom_size and wpturbo_featured_size in this case.
  2. The second and third arguments are the custom width and height of the image size in pixels; 800x600 and 1200x800 respectively.
  3. The fourth and final argument is a boolean that specifies whether the image should be cropped to the specified dimensions. The value true crops the image to the exact dimensions defined for the image size, while the value false will resize the image to fit within those dimensions while maintaining the aspect ratio.

Note that when creating new image sizes, consider the impact that it has on your website’s load times. Creating image sizes with very large dimensions can significantly slow down your website, so it is important to consider the sizes carefully.

Lastly, we add the wpturbo_add_image_sizes() function to the after_setup_theme hook using add_action(). This ensures that the image sizes are defined and become available on the website.

Register an account to save your snippets or go Pro to get more features.