@@ -358,6 +358,28 @@ function callback_file( $args ) {
358358 echo $ html ;
359359 }
360360
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+
361383 /**
362384 * Displays a password field for a settings field
363385 *
@@ -574,12 +596,17 @@ function(){
574596 file_frame.on('select', function () {
575597 attachment = file_frame.state().get('selection').first().toJSON();
576598
577- self.prev('.wpsa-url').val(attachment.url);
599+ self.prev('.wpsa-url').val(attachment.url).change() ;
578600 });
579601
580602 // Finally, open the modal
581603 file_frame.open();
582604 });
605+
606+ $('input.wpsa-url').on( 'change keyup paste input', (function() {
607+ var self = $(this);
608+ self.next().next().next( '.wpsa-image-preview' ).children( 'img' ).attr( 'src', self.val() );
609+ })).change();
583610 });
584611 </script>
585612
0 commit comments