Skip to content

Commit c453b20

Browse files
committed
Add image field type
The image field type stores media id. The media manager is limited to type image. Settings section shows a thumbnail preview. Signed-off-by: Roi Dayan <[email protected]>
1 parent d8ec650 commit c453b20

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

example/oop-example.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,17 @@ function get_settings_fields() {
127127
'type' => 'file',
128128
'default' => '',
129129
'options' => array(
130-
'button_label' => 'Choose Image'
130+
'button_label' => __( 'Choose File' )
131+
)
132+
),
133+
array(
134+
'name' => 'image',
135+
'label' => __( 'Image', 'wedevs' ),
136+
'desc' => __( 'Image description', 'wedevs' ),
137+
'type' => 'image',
138+
'default' => '',
139+
'options' => array(
140+
'button_label' => __( 'Choose Image' )
131141
)
132142
)
133143
),

src/class.settings-api.php

+58-6
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,37 @@ function callback_file( $args ) {
366366
$id = $args['section'] . '[' . $args['id'] . ']';
367367
$label = isset( $args['options']['button_label'] ) ? $args['options']['button_label'] : __( 'Choose File' );
368368

369-
$html = sprintf( '<input type="text" class="%1$s-text wpsa-url" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
369+
$html = sprintf( '<input type="text" class="%1$s-text wpsa-url" id="%2$s" name="%2$s" value="%3$s"/>', $size, $id, $value );
370370
$html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />';
371371
$html .= $this->get_field_description( $args );
372372

373373
echo $html;
374374
}
375375

376+
/**
377+
* Displays an image upload field with a preview
378+
*
379+
* @param array $args settings field args
380+
*/
381+
function callback_image( $args ) {
382+
383+
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
384+
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
385+
$id = $args['section'] . '[' . $args['id'] . ']';
386+
$label = isset( $args['options']['button_label'] ) ?
387+
$args['options']['button_label'] :
388+
__( 'Choose Image' );
389+
$img = wp_get_attachment_image_src( $value );
390+
$img_url = $img ? $img[0] : '';
391+
392+
$html = sprintf( '<input type="hidden" class="%1$s-text wpsa-image-id" id="%2$s" name="%2$s" value="%3$s"/>', $size, $id, $value );
393+
$html .= '<p class="wpsa-image-preview"><img src="' . $img_url . '" /></p>';
394+
$html .= '<input type="button" class="button wpsa-image-browse" value="' . $label . '" />';
395+
$html .= $this->get_field_description( $args );
396+
397+
echo $html;
398+
}
399+
376400
/**
377401
* Displays a password field for a settings field
378402
*
@@ -583,7 +607,6 @@ function(){
583607

584608
$('.wpsa-browse').on('click', function (event) {
585609
event.preventDefault();
586-
587610
var self = $(this);
588611

589612
// Create the media frame.
@@ -593,15 +616,44 @@ function(){
593616
text: self.data('uploader_button_text'),
594617
},
595618
multiple: false
596-
});
619+
})
597620

598-
file_frame.on('select', function () {
621+
.on('select', function () {
599622
attachment = file_frame.state().get('selection').first().toJSON();
600623
self.prev('.wpsa-url').val(attachment.url).change();
601-
});
624+
})
625+
626+
// Finally, open the modal
627+
.open();
628+
});
629+
630+
$('.wpsa-image-browse').on('click', function (event) {
631+
event.preventDefault();
632+
var self = $(this);
633+
634+
// Create the media frame.
635+
var file_frame = wp.media.frames.file_frame = wp.media({
636+
title: self.data('uploader_title'),
637+
button: {
638+
text: self.data('uploader_button_text'),
639+
},
640+
multiple: false,
641+
library: { type: 'image' }
642+
})
643+
644+
.on('select', function () {
645+
attachment = file_frame.state().get('selection').first().toJSON();
646+
var url;
647+
if (attachment.sizes && attachment.sizes.thumbnail)
648+
url = attachment.sizes.thumbnail.url;
649+
else
650+
url = attachment.url;
651+
self.parent().children('.wpsa-image-id').val(attachment.id).change();
652+
self.parent().children('.wpsa-image-preview').children('img').attr('src', url);
653+
})
602654

603655
// Finally, open the modal
604-
file_frame.open();
656+
.open();
605657
});
606658
});
607659
</script>

0 commit comments

Comments
 (0)