Skip to content

Instantly share code, notes, and snippets.

@scofennell
Last active August 29, 2015 14:01
Show Gist options
  • Save scofennell/a3d883b9d47c349abbd5 to your computer and use it in GitHub Desktop.
Save scofennell/a3d883b9d47c349abbd5 to your computer and use it in GitHub Desktop.
WordPress snippet for dealing with image sizes
<?php
/**
* Standard practice among WordPress themes is to declare the width of the main content column as a global.
*
* @var integer
*/
$content_width = 800;
/**
* A function to grab the $content_width and register several image sizes for our theme
*
* Caveats:
* * I'm basically assuming that everyone has a retina monitor, which is objectionable.
* * I'm assuming that desktop full-width is around 2000px, which is pretty random.
* * I'm assuming that the mobile world starts around 960px, which is highly questionalble.
*/
function sjf_deh_define_image_sizes() {
// Grab the $content_width global.
global $content_width;
add_image_size( 'sjf_deh_content_width', $content_width, 9999 );
// For retinas, the content width is doubled to arrive at full-width.
$retina_full = $content_width * 2;
add_image_size( 'sjf_deh_masonry_retina_full', $retina_full, 9999 );
// For retinas, the content width is used in full for half-width images.
$retina_half = $content_width;
add_image_size( 'sjf_deh_masonry_retina_half', $retina_half, 9999 );
// For retinas, the content width is halved for quarter-width images.
$retina_quarter = $content_width / 2;
add_image_size( 'sjf_deh_masonry_retina_quarter', $retina_quarter, 9999 );
// For full-page bg's lets say 2000px is a good width. Pretty arbitrary.
$sjf_bg_size = 2000;
add_image_size( 'sjf_deh_bg', $sjf_bg_size, 9999 );
// For full-page bg's for mobile folks, let's say 960px is a good width -- even more arbitrary.
$sjf_mobile_bg_size = 960;
add_image_size( 'sjf_deh_mobile_bg', $sjf_mobile_bg_size, 9999 );
}
add_action( 'init', 'sjf_deh_define_image_sizes' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment