@@ -363,13 +363,37 @@ function callback_file( $args ) {
363
363
$ id = $ args ['section ' ] . '[ ' . $ args ['id ' ] . '] ' ;
364
364
$ label = isset ( $ args ['options ' ]['button_label ' ] ) ? $ args ['options ' ]['button_label ' ] : __ ( 'Choose File ' );
365
365
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 );
367
367
$ html .= '<input type="button" class="button wpsa-browse" value=" ' . $ label . '" /> ' ;
368
368
$ html .= $ this ->get_field_description ( $ args );
369
369
370
370
echo $ html ;
371
371
}
372
372
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
+
373
397
/**
374
398
* Displays a password field for a settings field
375
399
*
@@ -580,7 +604,6 @@ function(){
580
604
581
605
$('.wpsa-browse').on('click', function (event) {
582
606
event.preventDefault();
583
-
584
607
var self = $(this);
585
608
586
609
// Create the media frame.
@@ -590,15 +613,44 @@ function(){
590
613
text: self.data('uploader_button_text'),
591
614
},
592
615
multiple: false
593
- });
616
+ })
594
617
595
- file_frame .on('select', function () {
618
+ .on('select', function () {
596
619
attachment = file_frame.state().get('selection').first().toJSON();
597
620
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
+ })
599
651
600
652
// Finally, open the modal
601
- file_frame .open();
653
+ .open();
602
654
});
603
655
});
604
656
</script>
0 commit comments