@@ -376,6 +376,30 @@ function callback_file( $args ) {
376
376
echo $ html ;
377
377
}
378
378
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
+
379
403
/**
380
404
* Displays a password field for a settings field
381
405
*
@@ -613,7 +637,6 @@ function(){
613
637
614
638
$('.wpsa-browse').on('click', function (event) {
615
639
event.preventDefault();
616
-
617
640
var self = $(this);
618
641
619
642
// Create the media frame.
@@ -623,15 +646,44 @@ function(){
623
646
text: self.data('uploader_button_text'),
624
647
},
625
648
multiple: false
626
- });
649
+ })
627
650
628
- file_frame .on('select', function () {
651
+ .on('select', function () {
629
652
attachment = file_frame.state().get('selection').first().toJSON();
630
653
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
+ })
632
684
633
685
// Finally, open the modal
634
- file_frame .open();
686
+ .open();
635
687
});
636
688
});
637
689
</script>
0 commit comments