Fixing Image Sizes on WordPress Attachment Pages

One of the neat things about WordPress is that when you upload an image and then include that image in a blog post, you can decide where that image links to. The image can link to nothing at all, the raw image, or an “attachment page” which contains that image and a caption.

That said, something that has caused me grief for out of the box WordPress builds has been the image on the media page being really small. Take for example, this picture of a freeloading cheetah. When I upload the picture, the attachment page looks like this:

Just look at that. A tiny image and a bunch of the page being completely unused. Disgraceful. Surely we can do better!

As it turns out, tweaking a single line of code can be used to change the size of all images on media pages.

Start by going to where WordPress is installed and opening the file wp-includes/post-template.php in your favorite editor. If you set up WordPress in Docker as detailed in a previous post of mine, WordPress lives under the wordpress/ directory so the full path would be wordpress/wp-includes/post-template.php.

Either way, once you have that file open, look for this line:

$p .= wp_get_attachment_link(0, 'medium', false);

Now, what you’re going to do is make a copy of that line, comment out the original line, and change “medium” to full. So the code will now look like this:

// $p .= wp_get_attachment_link(0, 'medium', false);
$p .= wp_get_attachment_link(0, 'full', false);

The reason why we are commenting out the original line is so that we can later roll things back if necessary. This is a good programming practice when you are tweaking something but not 100% sure if you’ll like the outcome.

Save the file, and you’re done! Trying to load the same attachment page now looks like this:

And that’s about all there is to it!

Got any questions? Let me know in the comments!