@@ -358,6 +358,28 @@ function callback_file( $args ) {
358
358
echo $ html ;
359
359
}
360
360
361
+ /**
362
+ * Displays an image upload field with a preview
363
+ *
364
+ * @param array $args settings field args
365
+ */
366
+ function callback_image ( $ args ) {
367
+
368
+ $ value = esc_attr ( $ this ->get_option ( $ args ['id ' ], $ args ['section ' ], $ args ['std ' ] ) );
369
+ $ size = isset ( $ args ['size ' ] ) && !is_null ( $ args ['size ' ] ) ? $ args ['size ' ] : 'regular ' ;
370
+ $ id = $ args ['section ' ] . '[ ' . $ args ['id ' ] . '] ' ;
371
+ $ label = isset ( $ args ['options ' ]['button_label ' ] ) ?
372
+ $ args ['options ' ]['button_label ' ] :
373
+ __ ( 'Choose Image ' );
374
+
375
+ $ 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 );
376
+ $ html .= '<input type="button" class="button wpsa-browse" value=" ' . $ label . '" /> ' ;
377
+ $ html .= $ this ->get_field_description ( $ args );
378
+ $ html .= '<p class="wpsa-image-preview"><img src=""/></p> ' ;
379
+
380
+ echo $ html ;
381
+ }
382
+
361
383
/**
362
384
* Displays a password field for a settings field
363
385
*
@@ -567,12 +589,17 @@ function(){
567
589
file_frame.on('select', function () {
568
590
attachment = file_frame.state().get('selection').first().toJSON();
569
591
570
- self.prev('.wpsa-url').val(attachment.url);
592
+ self.prev('.wpsa-url').val(attachment.url).change() ;
571
593
});
572
594
573
595
// Finally, open the modal
574
596
file_frame.open();
575
597
});
598
+
599
+ $('input.wpsa-url').on( 'change keyup paste input', (function() {
600
+ var self = $(this);
601
+ self.next().parent().children( '.wpsa-image-preview' ).children( 'img' ).attr( 'src', self.val() );
602
+ })).change();
576
603
});
577
604
</script>
578
605
0 commit comments