Skip to content

Commit 00c2ea7

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 243544e commit 00c2ea7

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
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

+57-5
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,30 @@ function callback_file( $args ) {
376376
echo $html;
377377
}
378378

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

614638
$('.wpsa-browse').on('click', function (event) {
615639
event.preventDefault();
616-
617640
var self = $(this);
618641

619642
// Create the media frame.
@@ -623,15 +646,44 @@ function(){
623646
text: self.data('uploader_button_text'),
624647
},
625648
multiple: false
626-
});
649+
})
627650

628-
file_frame.on('select', function () {
651+
.on('select', function () {
629652
attachment = file_frame.state().get('selection').first().toJSON();
630653
self.prev('.wpsa-url').val(attachment.url).change();
631-
});
654+
})
655+
656+
// Finally, open the modal
657+
.open();
658+
});
659+
660+
$('.wpsa-image-browse').on('click', function (event) {
661+
event.preventDefault();
662+
var self = $(this);
663+
664+
// Create the media frame.
665+
var file_frame = wp.media.frames.file_frame = wp.media({
666+
title: self.data('uploader_title'),
667+
button: {
668+
text: self.data('uploader_button_text'),
669+
},
670+
multiple: false,
671+
library: { type: 'image' }
672+
})
673+
674+
.on('select', function () {
675+
attachment = file_frame.state().get('selection').first().toJSON();
676+
var url;
677+
if (attachment.sizes && attachment.sizes.thumbnail)
678+
url = attachment.sizes.thumbnail.url;
679+
else
680+
url = attachment.url;
681+
self.parent().children('.wpsa-image-id').val(attachment.id).change();
682+
self.parent().children('.wpsa-image-preview').children('img').attr('src', url);
683+
})
632684

633685
// Finally, open the modal
634-
file_frame.open();
686+
.open();
635687
});
636688
});
637689
</script>

0 commit comments

Comments
 (0)