WP_Widget_Media_Image::render_media( array $instance )
Render the media on the frontend.
Description Description
Parameters Parameters
- $instance
-
(array) (Required) Widget instance props.
Return Return
(void)
Source Source
File: wp-includes/widgets/class-wp-widget-media-image.php
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'] ); } $image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? join( 'x', $size ) : $size ); $image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes ); $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]; } 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'] = ''; } $image = sprintf( '<img class="%1$s" src="%2$s" alt="%3$s" width="%4$s" height="%5$s" />', esc_attr( $classes ), esc_url( $instance['url'] ), esc_attr( $instance['alt'] ), esc_attr( $instance['width'] ), esc_attr( $instance['height'] ) ); } // 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 = wp_targeted_link_rel( $link ); } if ( $caption ) { $image = img_caption_shortcode( array( 'width' => $width, 'caption' => $caption, ), $image ); } echo $image; }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
4.8.0 | Introduced. |