When I upload a featured image, I want to give it the a width of “100%” but only if it is over 1170px. If the width is between 1170px and 770px, I want it be get a width of “770px”, otherwise the width will not be changed.
So far this code is doing what I want:
if (intval($width) >= 1170) {
$hwstring = 'width=100%';
} elseif ( (intval($width) < 1170) && (intval($width) >= 770) ) {
$hwstring = 'width=770px';
} else {
$hwstring = image_hwstring($width, 0);
};
However I have modified the media.php file inside the “wp-includes” folder which is apparently not the right way to do it. So how could I create a function that does the same thing without modifying the existing WordPress code?
function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {
$html = '';
$image = wp_get_attachment_image_src($attachment_id, $size, $icon);
if ( $image ) {
list($src, $width, $height) = $image;
$hwstring = image_hwstring($width, $height);
if ( is_array($size) )
$size = join('x', $size);
$attachment =& get_post($attachment_id);
$default_attr = array(
'src' => $src,
'class' => "attachment-$size",
'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
'title' => trim(strip_tags( $attachment->post_title )),
);
if ( empty($default_attr['alt']) )
$default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
if ( empty($default_attr['alt']) )
$default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title
$attr = wp_parse_args($attr, $default_attr);
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
$attr = array_map( 'esc_attr', $attr );
if (intval($width) >= 1170) {
$hwstring = 'width=100%';
} elseif ( (intval($width) < 1170) && (intval($width) >= 770) ) {
$hwstring = 'width=770px';
} else {
$hwstring = image_hwstring($width, 0);
};
$html = rtrim("<img $hwstring");
foreach ( $attr as $name => $value ) {
$html .= " $name=" . '"' . $value . '"';
}
$html .= ' />';
}
return $html;
}
Unfortunately there aren’t any hooks for you to use to do this exactly in that function, but you can construct it yourself without modifying the core WordPress files (which you don’t want to do lest you overwrite your custom code when you upgrade). I’m kind of surprised that the
wp_get_attachment_image_src()function doesn’t pass the return values through filter to do exactly what you are talking about.If you look at the top of this function, it gets the and array of
$src, $width, $heightby calling$image = wp_get_attachment_image_src($attachment_id, $size, $icon);You can make this same call yourself and construct your custom widths – basically copying the function into a custom version in functions.php or a custom functions plugin.You can create a new ticket at http://core.trac.wordpress.org/newticket if you want to request this functionality to be added in a future version. The addition would be towp_get_attachment_image_src()on line 515 (of the current trunk version of WP):EDIT: Ticket already exists, patch is a little weird, I submitted a new one but no guarantees as to when it will go in if it’s approved..