Skip to content

Instantly share code, notes, and snippets.

@EugeneCib
Created December 9, 2015 13:05
Show Gist options
  • Select an option

  • Save EugeneCib/e181234d05207685b9fb to your computer and use it in GitHub Desktop.

Select an option

Save EugeneCib/e181234d05207685b9fb to your computer and use it in GitHub Desktop.
WP function exapmle to make images use defaul image if no image exists or use exteranl tool for resizing images which are smaller then required sizes
<?php
function get_image_src_or_default($attachment_id, $image_size)
{
$image_size_info = false;
global $_wp_additional_image_sizes;
if(isset($_wp_additional_image_sizes[$image_size])) {
$image_size_info = $_wp_additional_image_sizes[$image_size];
}
$img = wp_get_attachment_image_src( $attachment_id, $image_size);
if(isset($img[0]) && $image_size_info)
{
if(($img[1] == $image_size_info['width']) && ($img[2] == $image_size_info['height']))
{
$src = $img[0];
}
else
{
$attachment_url = wp_get_attachment_url($attachment_id);
$src = SomeToolImageResizing::resizeImage($attachment_url, $image_size_info['width'], $image_size_info['height']);
}
}
else if($image_size_info)
{
$src = SomeToolImageResizing:::resizeImage('',$image_size_info['width'], $image_size_info['height']);
}
else
{
$src = '';
}
return $src;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment