16

I'm working on a theme that has a number of image sizes defined correctly using:

add_image_size( 'name', 500, 200, true );

I would like to override these defined sizes from the child theme:

add_image_size( 'new-name', 400, 300, true );

I know I can add new sizes, but if I add the same name i.e:

add_image_size( 'name', 400, 300, true );

Then the parent function call is used - the child one is ignored - adding extra rules adds extra images which will never be used - it's not so bad, but it's not efficient.

Is there a function to de-register an added image size?

Cheers!

Ray

2
  • Can you please post the entire code that is relevant? Are the add_image_size() calls just sitting nakedly in functions.php, or are they properly wrapped inside a callback, and hooked into after_setup_theme? Knowing which is critical to answering your question. Dec 4, 2012 at 16:31
  • hi chip - thanks for your comment - you're right it's wrapped in after_setup_theme - so this is all about priorities again - I always miss that!
    – Q Studio
    Dec 4, 2012 at 17:37

3 Answers 3

27

The add_image_size( $name, $width, $height, $crop ) function is graceful enough to handle multiple calls using the same $name. It simply overwrites the existing value:

$_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => (bool) $crop );

So that means that all you need to do to override the parent Theme's definition of the custom image size is to ensure that your call to add_image_size() fires after the parent Theme's call.

Assuming that the Parent Theme does like so:

function parent_theme_setup() {
    add_image_size( 'name', 500, 200, true );
    add_image_size( 'new-name', 400, 300, true );
}
add_action( 'after_setup_theme', 'parent_theme_setup' );

Then the parent Theme's add_image_size() call fires at the after_setup_theme hook, at the default priority (i.e. 10).

(Note: if the calls are not wrapped in a callback, then they fire at the plugins_loaded hook, at priority 10.)

Here's the critical part: the child Theme's functions.php file is parsed before the parent Theme's, so if you use the same hooks to fire your add_image_size() calls, they'll get overridden by the parent Theme's add_image_size() calls.

The solution is to use a later hook or a lower priority, to ensure that the child Theme's add_image_size() call fires after the parent Theme's.

This should do the trick, regardless of how the parent Theme fires its calls:

function child_theme_setup() {
    add_image_size( 'name', 400, 300, true );
}
add_action( 'after_setup_theme', 'child_theme_setup', 11 );

Note that we use the same after_setup_theme hook, but use a lower priority (i.e. 11 instead of 10. Callbacks fire in order from higher priority (lower number) to lower priority (higher number), starting with priority 0. So a callback hooked into priority 11 will fire after a callback hooked into priority 10.

Note also that the after_setup_theme hook itself fires after the plugins_loaded hook, so this callback will still override the parent Theme, even if the parent Theme is _doing_it_wrong() by not wrapping such calls in an appropriate callback, hooked into an appropriate hook.

3
  • excellent - thanks chip! - I had them both on the same priority - notched the child back after the parent and all working fine.
    – Q Studio
    Dec 4, 2012 at 17:41
  • I can't seem to overwrite the value if a plugin is calling add_image_size :(
    – Zhianc
    Apr 22, 2015 at 12:57
  • took a long time to find this answer. thank you! Jan 5, 2021 at 15:43
1

If someone wants to simply unset the additional image sizes, as opposed to changing the dimensions, use remove_image_size( string $name ), e.g.

function pinboard_child_theme_setup( $sizes) {

    $image_size_names = array('thumbnail', 'medium', 'large',
        'slider-thumb', 'blog-thumb', 'teaser-thumb', 'gallery-1-thumb',
        'gallery-2-thumb', 'gallery-3-thumb', 'image-thumb', 'video-thumb');

    $image_size_names_length = count($image_size_names);
    for($x = 0; $x < $image_size_names_length; $x++) {
        remove_image_size($image_size_names[$x]);
    }   
}
add_action( 'after_setup_theme', 'pinboard_child_theme_setup', 11);
-1

In your chile theme you would need to use add_action( 'after_setup_theme','new-name');

3
  • new-name here is the name of function or what ever name you are using for your main function
    – robin
    Dec 4, 2012 at 16:07
  • Add an explanation to your answer: why and how does it solve the problem? Note that new-name is an invalid callback name.
    – fuxia
    Dec 4, 2012 at 16:53
  • "new-name" is simply the handle of the image size being added.
    – Q Studio
    Dec 4, 2012 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.