@@ -363,13 +363,37 @@ function callback_file( $args ) {
363363 $ id = $ args ['section ' ] . '[ ' . $ args ['id ' ] . '] ' ;
364364 $ label = isset ( $ args ['options ' ]['button_label ' ] ) ? $ args ['options ' ]['button_label ' ] : __ ( 'Choose File ' );
365365
366- $ 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 );
366+ $ html = sprintf ( '<input type="text" class="%1$s-text wpsa-url" id="%2$s" name="%2$s" value="%3 $s"/> ' , $ size , $ id , $ value );
367367 $ html .= '<input type="button" class="button wpsa-browse" value=" ' . $ label . '" /> ' ;
368368 $ html .= $ this ->get_field_description ( $ args );
369369
370370 echo $ html ;
371371 }
372372
373+ /**
374+ * Displays an image upload field with a preview
375+ *
376+ * @param array $args settings field args
377+ */
378+ function callback_image ( $ args ) {
379+
380+ $ value = esc_attr ( $ this ->get_option ( $ args ['id ' ], $ args ['section ' ], $ args ['std ' ] ) );
381+ $ size = isset ( $ args ['size ' ] ) && !is_null ( $ args ['size ' ] ) ? $ args ['size ' ] : 'regular ' ;
382+ $ id = $ args ['section ' ] . '[ ' . $ args ['id ' ] . '] ' ;
383+ $ label = isset ( $ args ['options ' ]['button_label ' ] ) ?
384+ $ args ['options ' ]['button_label ' ] :
385+ __ ( 'Choose Image ' );
386+ $ img = wp_get_attachment_image_src ( $ value );
387+ $ img_url = $ img ? $ img [0 ] : '' ;
388+
389+ $ html = sprintf ( '<input type="hidden" class="%1$s-text wpsa-image-id" id="%2$s" name="%2$s" value="%3$s"/> ' , $ size , $ id , $ value );
390+ $ html .= '<p class="wpsa-image-preview"><img src=" ' . $ img_url . '" /></p> ' ;
391+ $ html .= '<input type="button" class="button wpsa-image-browse" value=" ' . $ label . '" /> ' ;
392+ $ html .= $ this ->get_field_description ( $ args );
393+
394+ echo $ html ;
395+ }
396+
373397 /**
374398 * Displays a password field for a settings field
375399 *
@@ -580,7 +604,6 @@ function(){
580604
581605 $('.wpsa-browse').on('click', function (event) {
582606 event.preventDefault();
583-
584607 var self = $(this);
585608
586609 // Create the media frame.
@@ -590,15 +613,44 @@ function(){
590613 text: self.data('uploader_button_text'),
591614 },
592615 multiple: false
593- });
616+ })
594617
595- file_frame .on('select', function () {
618+ .on('select', function () {
596619 attachment = file_frame.state().get('selection').first().toJSON();
597620 self.prev('.wpsa-url').val(attachment.url).change();
598- });
621+ })
622+
623+ // Finally, open the modal
624+ .open();
625+ });
626+
627+ $('.wpsa-image-browse').on('click', function (event) {
628+ event.preventDefault();
629+ var self = $(this);
630+
631+ // Create the media frame.
632+ var file_frame = wp.media.frames.file_frame = wp.media({
633+ title: self.data('uploader_title'),
634+ button: {
635+ text: self.data('uploader_button_text'),
636+ },
637+ multiple: false,
638+ library: { type: 'image' }
639+ })
640+
641+ .on('select', function () {
642+ attachment = file_frame.state().get('selection').first().toJSON();
643+ var url;
644+ if (attachment.sizes && attachment.sizes.thumbnail)
645+ url = attachment.sizes.thumbnail.url;
646+ else
647+ url = attachment.url;
648+ self.parent().children('.wpsa-image-id').val(attachment.id).change();
649+ self.parent().children('.wpsa-image-preview').children('img').attr('src', url);
650+ })
599651
600652 // Finally, open the modal
601- file_frame .open();
653+ .open();
602654 });
603655 });
604656 </script>
0 commit comments