Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f76a5a3

Browse files
committedMay 19, 2015
Add image field type
Works the same as file field type but with a preview of the image. Signed-off-by: Roi Dayan <roi.dayan@gmail.com>
1 parent 9901313 commit f76a5a3

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed
 

‎example/oop-example.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function get_settings_fields() {
129129
'name' => 'file',
130130
'label' => __( 'File', 'wedevs' ),
131131
'desc' => __( 'File description', 'wedevs' ),
132-
'type' => 'file',
132+
'type' => 'image',
133133
'default' => '',
134134
'options' => array(
135135
'button_label' => 'Choose Image'

‎src/class.settings-api.php

+28-1
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)
Please sign in to comment.