Change image thumbnail size in WordPress gallery

Change image thumbnail size in WordPress gallery

WordPress offers very useful built in functionality where you can specify image size for galleries within dashboard. But, what if you want to automatically change image size for all galleries? Here is a neat solution with just couple of lines of PHP code.

Understanding WordPress native gallery system

First, let’s take a look at how WordPress gallery system works. By default, WordPress provides you with 3 basic image sizes you can modify in Media Settings. See an example below:

meks_wp_media_settings

Whilst creating a gallery inside your post/page content, you are able to choose image size you want to use for particular gallery. Four sizes are available by default: Thumbnail, Medium, Large and Full (originally uploaded image).

meks_wp_edit_gallery

All right, so far so good! But, imagine that you have just switched to a brand new WordPress theme which has a totaly different styling. Obviously, you don’t want to go back to each gallery and change appropriate size settings one by one. Here’s a solution.

Introducing shortcode_atts_gallery filter

This simple snippet will do the trick. Just take a look at the example below:

And here is one addition to previous snippet. For example, if you want to use specific image size depending on the number of columns set for a particular gallery. This may be quite useful so the images use appropriate sizes ( i.e. instead of using big images if you have thumbnail styled gallery).

Ok, that’s it! Just add this snippet to your theme functions.php file and modify snippet for your needs.

Live example?

We have used this approach inside our Throne WordPress Theme so feel free to take a look and see how galleries with different number of columns use different image sizes.

Bojan Petrovic

Co-founder and WordPress developer at Meks. He has been involved in the business since 2009. creating themes, plugins and services on top of WordPress, continuously aiming to choose best approaches and work smarter, not harder.

10 comments

Leave a comment
  • Super easy solution for irritating problem. Thanks!:) Your blog is the best suorce for tips like that, I put it in my bookmarks. And hope that you start writing a little bit more often.

  • Hi there,
    I was wondering if you would know of a way that I can use full image size for all existing posts? I have an issue where something between Jetpack’s Photon and changing themes (or something else entirely!?) has broken almost 1000 images. They only show up when changed over to “full” individually.

    Before I take that one, I was hoping that you might know of a way to change that via code. This would unfortunately apply to galleries, too, I suppose, but better to have weird infrequent galleries than almost 1,000 broken images, I guess.

  • Hi Grace,

    This may be an issue caused by a lot of thing so there is no “silver bullet” solution we can recommend unfortunately. I’m not sure if you are familiar with our work but we do provide WordPress customization services so if you are interested feel free to contact us and we would be glad to have a look.

    Cheers

  • Hi Meks, Really a very nice and result-oriented tutorial. but I don’t want all 4 default images sizes generated by wordpress. Should I delete the unnecessary image sizes (medium and large) to save disk space and reducing server load. What’s your opinion?

Leave a Reply

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