Setting max image size for your WordPress themes

WordPress 2.5 includes some code designed to prevent oversized images from breaking up the layout of themes by spilling outside of content areas.  The code is found in the media.php file and looks like this:

The important part of the code is this bit:

		if ( !empty($GLOBALS['content_width']) ) {
			$max_width = $GLOBALS['content_width'];
		}
		else
			$max_width = 500;

What is means is if no global value has been set (either in the WordPress install or in the theme itself) to default to a max width of 500 pixels for any images that are inserted as “full size”.

Here at Crane Factory my current theme will support images up to 600px wide, and that is the size I generally shrink any large files down to before uploading them.  Unless I tell WordPress this it will keep shrinking “full size” images to 500px when I insert them in posts.  To fix this I simply add create a functions.php file in the folder for my theme and add the following code to it:

$GLOBALS['content_width'] = 600;

I have no other functions.php code so the whole file looks like this:

Now when I insert images 600px or more wide, WordPress will shrink them down to 600px to fit my theme rather than the default 500px.

Say thanks by sharing this post with your friends

About Paul Cunningham

Paul has been blogging since 2006, runs a popular technology website, and is the author of several ebooks. Read more about him here, and follow him on Twitter at @paulcunningham.

Comments

  1. Brilliant!

  2. Dan says:

    That was very useful! Thanks a million!

Trackbacks

  1. [...] Medium to Large, and then insert the image.  I also had to ensure that any of my themes contained code to define the dimensions of the “Large” image size as only the “Medium” size could be changed in the admin [...]