Skip to content

Instantly share code, notes, and snippets.

@solepixel
Created August 25, 2013 14:48
Show Gist options
  • Save solepixel/6334233 to your computer and use it in GitHub Desktop.
Save solepixel/6334233 to your computer and use it in GitHub Desktop.
Suggested change for Tri.be Image Widget plugin, image-widget/image-widget.php, See line 32 and 84 for changes.
<?php
class Tribe_Image_Widget extends WP_Widget {
// ...
private function get_image_html( $instance, $include_link = true ) {
// Backwards compatible image display.
if ( $instance['attachment_id'] == 0 && $instance['image'] > 0 ) {
$instance['attachment_id'] = $instance['image'];
}
$output = '';
if ( $include_link && !empty( $instance['link'] ) ) {
$attr = array(
'href' => $instance['link'],
'target' => $instance['linktarget'],
'class' => $this->widget_options['classname'].'-image-link',
'title' => ( !empty( $instance['alt'] ) ) ? $instance['alt'] : $instance['title'],
);
$attr = apply_filters('image_widget_link_attributes', $attr, $instance );
$attr = array_map( 'esc_attr', $attr );
$output = '<a';
foreach ( $attr as $name => $value ) {
$output .= sprintf( ' %s="%s"', $name, $value );
}
$output .= '>';
}
$output = apply_filters( 'image_widget_before_image', $output, $instance );
$size = $this->get_image_size( $instance );
if ( is_array( $size ) ) {
$instance['width'] = $size[0];
$instance['height'] = $size[1];
} elseif ( !empty( $instance['attachment_id'] ) ) {
//$instance['width'] = $instance['height'] = 0;
$image_details = wp_get_attachment_image_src( $instance['attachment_id'], $size );
if ($image_details) {
$instance['imageurl'] = $image_details[0];
$instance['width'] = $image_details[1];
$instance['height'] = $image_details[2];
}
}
$instance['width'] = abs( $instance['width'] );
$instance['height'] = abs( $instance['height'] );
$attr = array();
$attr['alt'] = $instance['title'];
if (is_array($size)) {
$attr['class'] = 'attachment-'.join('x',$size);
} else {
$attr['class'] = 'attachment-'.$size;
}
$attr['style'] = '';
if (!empty($instance['maxwidth'])) {
$attr['style'] .= "max-width: {$instance['maxwidth']};";
}
if (!empty($instance['maxheight'])) {
$attr['style'] .= "max-height: {$instance['maxheight']};";
}
if (!empty($instance['align']) && $instance['align'] != 'none') {
$attr['class'] .= " align{$instance['align']}";
}
$attr = apply_filters( 'image_widget_image_attributes', $attr, $instance );
// If there is an imageurl, use it to render the image. Eventually we should kill this and simply rely on attachment_ids.
if ( !empty( $instance['imageurl'] ) ) {
// If all we have is an image src url we can still render an image.
$attr['src'] = $instance['imageurl'];
$attr = array_map( 'esc_attr', $attr );
$hwstring = image_hwstring( $instance['width'], $instance['height'] );
$output .= rtrim("<img $hwstring");
foreach ( $attr as $name => $value ) {
$output .= sprintf( ' %s="%s"', $name, $value );
}
$output .= ' />';
} elseif( abs( $instance['attachment_id'] ) > 0 ) {
$output .= wp_get_attachment_image($instance['attachment_id'], $size, false, $attr);
}
$output = apply_filters( 'image_widget_after_image', $output, $instance );
if ( $include_link && !empty( $instance['link'] ) ) {
$output .= '</a>';
}
return $output;
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment