Automatically Set WordPress Thumbnail Sizes When Switching Themes

Right now, one of my main projects at work is a totally new theme for the official news outlet for a major corporation. Along with a million other changes, one thing that needs to happen ASAP after switching to the new theme is that the image thumbnail sizes – controlled through Settings › Media in wp-admin – need to be changed.

Of course, being the lazy, automation-obsessed developer that I am, I wanted to find a way to automatically set these thumbnail sizes the instant we changed themes. Fortunately, WordPress fires the after_switch_theme as soon as the new theme is set; with that action, we can add something like the following to our new theme’s functions.php file:

Now, as soon as we switch themes, we’ll automatically change the sizes stored in our wp_options table, based on our $image_sizes array.

With great power…

Of course, just because you can do something doesn’t mean that you should. If you’re building a new, custom theme, automatically setting thumbnail sizes like this might be super useful. If you’re releasing something publicly, a technique like this starts bordering on “eh, maybe you’re overstepping your bounds as a theme developer.” I’m not here to make that call, I just came here to chew gum and build themes (and I’m all outta gum).

Leave a Reply