Last updated on October 14, 2023

Modify the Default Image Size Dimensions

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Optimize images with custom sizes.

The code snippet below demonstrates how to modify the default image size dimensions in WordPress. This can be useful when you want to customize the default image sizes to better fit your theme or specific design requirements.

function wpsnippets_custom_image_sizes( $sizes ) {
    // Modify the default image sizes
    $sizes['thumbnail'] = array(
        'width'  => 150,
        'height' => 150,
        'crop'   => true,
    );
    $sizes['medium'] = array(
        'width'  => 300,
        'height' => 300,
        'crop'   => false,
    );
    $sizes['large'] = array(
        'width'  => 800,
        'height' => 800,
        'crop'   => false,
    );

    return $sizes;
}
add_filter( 'intermediate_image_sizes_advanced', 'wpsnippets_custom_image_sizes' );

In the code snippet above, we define a custom function wpsnippets_custom_image_sizes that takes an array of image sizes as a parameter. We then modify the default image sizes by updating the values of the thumbnail, medium, and large sizes within the array.

For example, we set the thumbnail size to have a width of 150 pixels, a height of 150 pixels, and enable cropping. The medium size is set to have a width of 300 pixels, a height of 300 pixels, and no cropping. The large size is set to have a width of 800 pixels, a height of 800 pixels, and no cropping.

Finally, we hook our custom function to the intermediate_image_sizes_advanced filter using the add_filter function. This ensures that our custom image sizes are used instead of the default sizes when generating images in WordPress.

By modifying the default image size dimensions, you can tailor the image sizes to better suit your theme or design requirements, resulting in more visually appealing and optimized images on your WordPress site.

Examples

Example 1: Modifying the default image size dimensions in functions.php

This use case demonstrates how to modify the default image size dimensions in WordPress by adding custom code to the functions.php file of your theme.

function wpsnippets_custom_image_sizes() {
    add_image_size( 'custom-thumbnail', 400, 300, true );
}
add_action( 'after_setup_theme', 'wpsnippets_custom_image_sizes' );

In this code example, we define a custom image size called ‘custom-thumbnail’ with dimensions of 400 pixels width and 300 pixels height. The fourth parameter true indicates that the image should be cropped to fit these dimensions. The add_image_size() function is hooked to the after_setup_theme action, ensuring that the image size is registered when the theme is set up.

Example 2: Modifying the default image size dimensions using a plugin

This use case demonstrates how to modify the default image size dimensions in WordPress by using a plugin.

function wpsnippets_modify_image_sizes() {
    update_option( 'thumbnail_size_w', 400 );
    update_option( 'thumbnail_size_h', 300 );
    update_option( 'thumbnail_crop', 1 );
}
add_action( 'init', 'wpsnippets_modify_image_sizes' );

In this code example, we use the update_option() function to modify the default thumbnail size dimensions. We set the width to 400 pixels, the height to 300 pixels, and enable cropping by setting the thumbnail_crop option to 1. The wpsnippets_modify_image_sizes() function is hooked to the init action, ensuring that the modifications are applied during the initialization of WordPress.

Example 3: Modifying the default image size dimensions using a plugin with a UI

This use case demonstrates how to modify the default image size dimensions in WordPress by using a plugin that provides a user interface (UI) for managing image sizes.

function wpsnippets_modify_image_sizes() {
    add_image_size( 'custom-thumbnail', 400, 300, true );
}
add_action( 'after_setup_theme', 'wpsnippets_modify_image_sizes' );

In this code example, we use the add_image_size() function to add a custom image size called ‘custom-thumbnail’ with dimensions of 400 pixels width and 300 pixels height. The fourth parameter true indicates that the image should be cropped to fit these dimensions. This code can be added to a custom plugin that provides a UI for managing image sizes, allowing users to easily modify the default image size dimensions.

Last updated on October 14, 2023. Originally posted on October 14, 2023.

Leave a Reply

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