WP_Widget_Media_Image::render_media( array $instance )

In this article

Render the media on the frontend.

Parameters

$instancearrayrequired
Widget instance props.

Source

public function render_media( $instance ) {	$instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );	$instance = wp_parse_args(	$instance,	array(	'size' => 'thumbnail',	)	);	$attachment = null;	if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {	$attachment = get_post( $instance['attachment_id'] );	}	if ( $attachment ) {	$caption = '';	if ( ! isset( $instance['caption'] ) ) {	$caption = $attachment->post_excerpt;	} elseif ( trim( $instance['caption'] ) ) {	$caption = $instance['caption'];	}	$image_attributes = array(	'class' => sprintf( 'image wp-image-%d %s', $attachment->ID, $instance['image_classes'] ),	'style' => 'max-width: 100%; height: auto;',	);	if ( ! empty( $instance['image_title'] ) ) {	$image_attributes['title'] = $instance['image_title'];	}	if ( $instance['alt'] ) {	$image_attributes['alt'] = $instance['alt'];	}	$size = $instance['size'];	if ( 'custom' === $size || ! in_array( $size, array_merge( get_intermediate_image_sizes(), array( 'full' ) ), true ) ) {	$size = array( $instance['width'], $instance['height'] );	$width = $instance['width'];	} else {	$caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );	$width = empty( $caption_size[0] ) ? 0 : $caption_size[0];	}	$image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? implode( 'x', $size ) : $size );	$image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );	} else {	if ( empty( $instance['url'] ) ) {	return;	}	$instance['size'] = 'custom';	$caption = $instance['caption'];	$width = $instance['width'];	$classes = 'image ' . $instance['image_classes'];	if ( 0 === $instance['width'] ) {	$instance['width'] = '';	}	if ( 0 === $instance['height'] ) {	$instance['height'] = '';	}	$attr = array(	'class' => $classes,	'src' => $instance['url'],	'alt' => $instance['alt'],	'width' => $instance['width'],	'height' => $instance['height'],	);	$loading_optimization_attr = wp_get_loading_optimization_attributes(	'img',	$attr,	'widget_media_image'	);	$attr = array_merge( $attr, $loading_optimization_attr );	$attr = array_map( 'esc_attr', $attr );	$image = '<img';	foreach ( $attr as $name => $value ) {	$image .= ' ' . $name . '="' . $value . '"';	}	$image .= ' />';	} // End if().	$url = '';	if ( 'file' === $instance['link_type'] ) {	$url = $attachment ? wp_get_attachment_url( $attachment->ID ) : $instance['url'];	} elseif ( $attachment && 'post' === $instance['link_type'] ) {	$url = get_attachment_link( $attachment->ID );	} elseif ( 'custom' === $instance['link_type'] && ! empty( $instance['link_url'] ) ) {	$url = $instance['link_url'];	}	if ( $url ) {	$link = sprintf( '<a href="%s"', esc_url( $url ) );	if ( ! empty( $instance['link_classes'] ) ) {	$link .= sprintf( ' class="%s"', esc_attr( $instance['link_classes'] ) );	}	if ( ! empty( $instance['link_rel'] ) ) {	$link .= sprintf( ' rel="%s"', esc_attr( $instance['link_rel'] ) );	}	if ( ! empty( $instance['link_target_blank'] ) ) {	$link .= ' target="_blank"';	}	$link .= '>';	$link .= $image;	$link .= '</a>';	$image = $link;	}	if ( $caption ) {	$image = img_caption_shortcode(	array(	'width' => $width,	'caption' => $caption,	),	$image	);	}	echo $image; } 

Changelog

VersionDescription
4.8.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.