When [gallery link="file"] is used, it calls the original image file source.

It it possible to create a shortcode filter that links to a specific size, like:

[gallery link="medium"]


[gallery link="custom-size-name"]

(referring to an additional image size created with add_image_size() in functions.php.)

  • core.trac.wordpress.org/ticket/18178 is different patch for [gallery] shortcode enhancement. From the looks of it, I think patching this feature into the core shouldn't be much of an issue complexity wise. – lkraav Mar 15 '12 at 21:10
  • +1. even 3 years later, and after the recent gallery revamps, this is still an issue - especially if you use a lightbox in conjunction with the wordpress gallery. there needs to be a size option here to stop users having to download 4 meg images to view in a 800x600 lightbox... – jammypeach Oct 7 '14 at 9:51

I found something very close that works here:



Short of de-registering the built in shortcode tag, copying the function and re-registering it with some modifications you could filter on wp_get_attachment_link. The problem is this affects everywhere that function is used.

Saying that it might not be such a bad thing:

add_filter( 'wp_get_attachment_link', 'my_attachment_link', 10, 6 );
function my_attachment_link( $link, $id, $size, $permalink, $icon, $text ) {
    if ( !is_admin() ) {
        // if we're linking to the file itself
        if ( ! $permalink )
            $link = preg_replace("/href=\'([^\']+)\'/", "href='" . wp_get_attachment_image_src( $id, 'my-custom-size', false ) . "'", $link );
    return $link;

This will override any links generated to the original size file on the front end with a link to the size you specify in the second argument in wp_get_attachment_image_src().

  • Just realised this is basically what the oikos.org.uk example is – sanchothefat Mar 8 '12 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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