Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing For Custom Images Sizes with WordPress's Gallery Shortcode

In WordPress 2.5 and up, there's a built in Gallery feature that allows the option to add an image gallery to a Post or Page on your WordPress blog. (Ref: http://codex.wordpress.org/Gallery_Shortcode)

You can use a size option to specify the thumbnail size you would like displayed. Valid values include "thumbnail", "medium", "large" and "full". The default is "thumbnail". The size of the images for "thumbnail", "medium" and "large" can be configured in WordPress admin panel.

ie. [gallery size="medium"]

My Question: I'm trying to hack up the [gallery] shortcode to allow for custom sizes at the time of input -- not trying to do this through the admin panel. I'd like to use something like, [gallery size="145x160"].

Rather then download a bloated plugin, I'd rather work with what's already there and I'm not sure where I need to go in my file structure to make the changes. I'm familiar with PHP but I'm afraid I'll make a change and then when I update future versions of WP, it will overwrite what I've set in motion.
Could someone help me out with this?

Thank you very much!

like image 727
Mike B. Avatar asked Aug 25 '10 16:08

Mike B.


1 Answers

I know this is late, but I found this question trying to accomplish the same thing.

The Gallery does not have any built-in filters to allow this, so I developed a solution that works below.

In your theme's functions.php file, add the following lines of code:

remove_shortcode('gallery');
add_shortcode('gallery', 'custom_size_gallery');

function custom_size_gallery($attr) {
    // Change size here - medium, large, full
    $attr['size'] = 'medium';
    return gallery_shortcode($attr);
}

This will interrupt the normal gallery call, revise the size being used, and then call the built-in WordPress gallery.

like image 184
random_user_name Avatar answered Sep 19 '22 19:09

random_user_name