Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.)

share|improve this question
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

3 Answers 3

You obviously didn't look in the Codex before you asked this. Check out Gallery Shortcode from within the Codex. You can set the attribute size to whatever you want.

share|improve this answer
I did look in the Codex, but the only thing specified for the link attribute is: "you can set it to "file" so each image will link to the image file. The default value links to the attachment's permalink." There's no information on setting it to a specific image source link. Did I miss something? –  Marc P Nov 9 '11 at 17:18
I should also clarify: I don't have a problem with the display of the thumbnails at different size. I want to link to a specific file size when the user clicks on the thumbnails. –  Marc P Nov 9 '11 at 17:27

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().

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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