How to change the target size of images clicked on in WordPress standard gallery

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 .

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:

<?php

$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!
wp_reset_postdata();

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

echo do_shortcode( '' );

?>

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

Solutions Collecting From Web of "How to change the target size of images clicked on in WordPress standard gallery"

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