@@ -366,13 +366,37 @@ function callback_file( $args ) {
366
366
$ id = $ args ['section ' ] . '[ ' . $ args ['id ' ] . '] ' ;
367
367
$ label = isset ( $ args ['options ' ]['button_label ' ] ) ? $ args ['options ' ]['button_label ' ] : __ ( 'Choose File ' );
368
368
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 );
370
370
$ html .= '<input type="button" class="button wpsa-browse" value=" ' . $ label . '" /> ' ;
371
371
$ html .= $ this ->get_field_description ( $ args );
372
372
373
373
echo $ html ;
374
374
}
375
375
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
+
376
400
/**
377
401
* Displays a password field for a settings field
378
402
*
@@ -583,7 +607,6 @@ function(){
583
607
584
608
$('.wpsa-browse').on('click', function (event) {
585
609
event.preventDefault();
586
-
587
610
var self = $(this);
588
611
589
612
// Create the media frame.
@@ -593,15 +616,44 @@ function(){
593
616
text: self.data('uploader_button_text'),
594
617
},
595
618
multiple: false
596
- });
619
+ })
597
620
598
- file_frame .on('select', function () {
621
+ .on('select', function () {
599
622
attachment = file_frame.state().get('selection').first().toJSON();
600
623
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
+ })
602
654
603
655
// Finally, open the modal
604
- file_frame .open();
656
+ .open();
605
657
});
606
658
});
607
659
</script>
0 commit comments