Overriding images sizes in a WordPress child theme

The WordPress function add_image_size allows the declaration of a size name to specific dimensions (see WordPress reference). Generally, these are declared in the functions.php file.  But what if there is a declaration in the parent theme that needs to be changed in the child theme?  Unfortunately, in this case, it will not be overwritten.   To do so, add_image_size needs to be defined in a function on both the parent and child.

In the parent functions.php:

function parent_image_size () {
add_image_size( ‘name-one’, 600, 300, true );

}
add_action( ‘after_setup_theme’, ‘parent_image_size’ ,10);

In the child functions.php:

function child_image_size() {
add_image_size( ‘name-one’, 300, 200, true );
}
add_action( ‘after_setup_theme’, ‘child_image_size’, 11 );

This scenario works by using the add_action priority attribute.  The child function will execute after the parent function, thus using the child size(s).

If existing images need to be resized, you will need to run a regenerate images plugin.

Plugin Solution

In lieu of the code above, a comprehensive plugin that works well is Simple Image Sizes. This plugin will display a table of all defined image sizes and allow change the sizes.  In addition, it feature a image regeneration with lots of options for where to apply the sizing change.

Leave a Reply

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

I accept the Privacy Policy

This site uses Akismet to reduce spam. Learn how your comment data is processed.