WebHostingPeople

WebHostingPeople
Table of Contents
Print

How to Add Custom Images Sizes to WordPress

As a default, the WordPress media library saves all uploaded pictures in 4 different variations. These consist of thumbnail, moderate, big, and full-size iterations of the image. However, for certain websites, it might be necessary to automatically resize all images to specific dimensions. In such cases, the default options might not always meet your requirements. To include a {custom} image size, you need to modify the Theme Features file in your active WordPress theme. This file is referred to as ‘capabilities.php’ and is located in the theme’s root directory. Fortunately, you can effortlessly modify the contents of this file from within WordPress itself, without the use of a text editor or any separate program. The function you will employ to add {custom} image sizes is known as “add_image_size”. The process is elaborate as follows:

  1. Sign in to your WordPress administrator dashboard, proceed to the “Appearance” tab on the sidebar, and click on “Editor”. You will be directed to the Edit Themes page, which will display the style sheet for the currently selected theme.
  2. Scroll through the list of templates until you find “Theme Features (capabilities.php)” and select it.
  3. Scroll to the bottom of the file and copy and paste the following code:

if ( function_exists( ‘add_image_size’ ) )

add_image_size( ‘custom-size’, 320, 160, true ); //(cropped)

add_filter(‘image_size_names_choose’, ‘my_image_sizes’);

function my_image_sizes($sizes)

$addsizes = array(

“new-size” => __( “New Size”)

);

$newsizes = array_merge($sizes, $addsizes);

return $newsizes;

Replace ‘custom-size’ on the second line of this code with your preferred name for the new size. Make sure to use dashes instead of spaces. Substitute the two numbers after that with the desired dimensions in pixels for the new image size (width by height). For adding additional image sizes, simply repeat the second line and enter the new details into the code.

  1. Click on “Update File”. If you entered the code correctly, a message will appear at the top of the page stating “File edited successfully”.
  2. When you upload an image in a new or existing post, you will now see the new image size listed in the dropdown box next to “Size”. This custom size will only be available for new images added to your WordPress media library.
    How To Add Custom Images Sizes To Wordpress

You can find more information about the add_image_size function at codex.wordpress.org/Function_Reference/add_image_size.

Post Your Comment

Categories