WP_Widget_Media_Gallery::form( array $instance )
Back-end widget form.
Description
See also
Parameters
- $instance
-
(Required) Previously saved values from database.
Source
File: wp-includes/widgets/class-wp-widget-media-gallery.php
public function form( $instance ) {
$instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
$ids = ! empty( $instance['ids'] ) ? implode( ',', $instance['ids'] ) : '';
$columns = ! empty( $instance['columns'] ) ? $instance['columns'] : 3;
$size = ! empty( $instance['size'] ) ? $instance['size'] : 'thumbnail';
$link_type = ! empty( $instance['link_type'] ) ? $instance['link_type'] : 'post';
$orderby_random = ! empty( $instance['orderby_random'] ) ? $instance['orderby_random'] : false;
$nonce = wp_create_nonce( '_wpnonce' );
?>
<div class="media-widget-control selected">
<fieldset>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:' ); ?></label>
<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" data-property="title" class="widefat" value="<?php echo esc_attr( $title ); ?>">
</fieldset>
<?php
$files_exist = false;
if ( $ids ) {
foreach ( $instance['ids'] as $id ) {
if ( file_exists( get_attached_file( $id ) ) ) {
$files_exist = true;
break;
}
}
}
if ( $ids && $files_exist ) {
$gallery_html = '<ul class="gallery media-widget-gallery-preview">';
foreach ( $instance['ids'] as $id ) {
$attributes = '';
$thumbnail = wp_get_attachment_image_src( $id, 'thumbnail', false );
$alt = get_post_meta( $id, '_wp_attachment_image_alt', true );
if ( $thumbnail && $thumbnail[0] ) {
// Create an aria-label attribute if the image has no alt attribute.
if ( $alt === '' ) {
$aria_label = esc_attr(
sprintf(
/* translators: %s: The image file name. */
__( 'The current image has no alternative text. The file name is: %s' ),
basename( $thumbnail[0] )
)
);
$attributes .= ' aria-label="' . $aria_label . '"';
}
$gallery_html .= '<li class="gallery-item">';
$gallery_html .= '<div class="gallery-icon">';
$gallery_html .= '<img alt="' . $alt . '" src="' . $thumbnail[0] . '" width="150" height="150"' . $attributes . '>';
$gallery_html .= '</div>';
$gallery_html .= '</li>';
}
}
$gallery_html .= '</ul>';
?>
<div class="media-widget-preview media_gallery"><?php echo $gallery_html; ?></div>
<fieldset class="media-widget-buttons">
<button type="button" class="button edit-media selected" data-edit-nonce="<?php echo esc_attr( $nonce ); ?>" style="margin-top:0;"><?php esc_html_e( 'Edit Gallery' ); ?></button>
</fieldset>
<?php
} else {
?>
<div class="media-widget-preview media_gallery">
<div class="attachment-media-view">
<button type="button" class="select-media button-add-media" data-edit-nonce="<?php echo esc_attr( $nonce ); ?>"><?php esc_html_e( 'Add Images' ); ?></button>
</div>
</div>
<?php
}
?>
<input id="<?php echo esc_attr( $this->get_field_id( 'ids' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'ids' ) ); ?>" type="hidden" data-property="ids" class="media-widget-instance-property" value="<?php echo esc_attr( $ids ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'columns' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'columns' ) ); ?>" type="hidden" data-property="columns" class="media-widget-instance-property" value="<?php echo esc_attr( esc_attr( $columns ) ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'size' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'size' ) ); ?>" type="hidden" data-property="size" class="media-widget-instance-property" value="<?php echo esc_attr( esc_attr( $size ) ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'link_type' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link_type' ) ); ?>" type="hidden" data-property="link_type" class="media-widget-instance-property" value="<?php echo esc_attr( $link_type ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'orderby_random' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'orderby_random' ) ); ?>" type="hidden" data-property="orderby_random" class="media-widget-instance-property" value="<?php echo esc_attr( $orderby_random ); ?>">
</div>
<?php
}
Changelog
| Version | Description |
|---|---|
| CP-2.5.0 | Introduced. CP-2.5.0 |