I want to use the standard Wordpress gallery without using other plugins and stick to the gallery shortcode. So, I know I can change the size of the thumbnails, that are shown in the gallery grid with for example [gallery size="xyz" link="file"].

But I am not satisfied with Wordpress opening the biggest file then, when clicking on one of the thumbnails and I would like to have stated a different size, which I have created upfront by using add_image_size( 'galleryimage', 900, 900 ); in my child's functions.php, of course.

Here is the code I am using for generating the galleries on static pages:


$people_attachments = new WP_Query( array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => 0,
'category_name' => 'people', // note: use category SLUG
) );

$people_id_array = array();

if ( $people_attachments->have_posts() ) : while ( $people_attachments->have_posts() ) : $people_attachments->the_post();

$people_id_array[] = get_the_ID();

endwhile; endif;
// Important!

$people_ids = implode( ',', $people_id_array );

echo do_shortcode( '[gallery link="file" columns="5" order="DESC" orderby="ID" include="' . $people_ids . '"]' );


How do I add the target image size (size of linked image to be shown, when clicked)?

1 Answer 1


I came across a solution for your functions.php from the following site that looks like it will do the trick. More detail on the implementation here:

Snippet below:

function oikos_get_attachment_link_filter( $content, $post_id, $size, $permalink ) {

// Only do this if we're getting the file URL
if (! $permalink) {
    // This returns an array of (url, width, height)
    $image = wp_get_attachment_image_src( $post_id, 'galleryimage' );
    $new_content = preg_replace('/href=\'(.*?)\'/', 'href=\'' . $image[0] . '\'', $content );
    return $new_content;
} else {
    return $content;

add_filter('wp_get_attachment_link', 'oikos_get_attachment_link_filter', 10, 4);
  • 1
    Thanks, that seems to work. Some guys wrote, it does not work in newer Wordpress versions (since 3.7/3.8), but I have it now on 3.9 it looks fine. Thank you again. :)
    – vega
    Commented Apr 18, 2014 at 19:41
  • Just another data point, worked for me in 4.6.1 without issues. Make sure to change galleryimage to your desired image size. Commented Nov 29, 2016 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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