Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image field type #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion example/oop-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,17 @@ function get_settings_fields() {
'type' => 'file',
'default' => '',
'options' => array(
'button_label' => 'Choose Image'
'button_label' => __( 'Choose File' )
)
),
array(
'name' => 'image',
'label' => __( 'Image', 'wedevs' ),
'desc' => __( 'Image description', 'wedevs' ),
'type' => 'image',
'default' => '',
'options' => array(
'button_label' => __( 'Choose Image' )
)
)
),
Expand Down
62 changes: 57 additions & 5 deletions src/class.settings-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,30 @@ function callback_file( $args ) {
echo $html;
}

/**
* Displays an image upload field with a preview
*
* @param array $args settings field args
*/
function callback_image( $args ) {

$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
$id = $args['section'] . '[' . $args['id'] . ']';
$label = isset( $args['options']['button_label'] ) ?
$args['options']['button_label'] :
__( 'Choose Image' );
$img = wp_get_attachment_image_src( $value );
$img_url = $img ? $img[0] : '';

$html = sprintf( '<input type="hidden" class="%1$s-text wpsa-image-id" id="%2$s" name="%2$s" value="%3$s"/>', $size, $id, $value );
$html .= '<p class="wpsa-image-preview"><img src="' . $img_url . '" /></p>';
$html .= '<input type="button" class="button wpsa-image-browse" value="' . $label . '" />';
$html .= $this->get_field_description( $args );

echo $html;
}

/**
* Displays a password field for a settings field
*
Expand Down Expand Up @@ -613,7 +637,6 @@ function(){

$('.wpsa-browse').on('click', function (event) {
event.preventDefault();

var self = $(this);

// Create the media frame.
Expand All @@ -623,15 +646,44 @@ function(){
text: self.data('uploader_button_text'),
},
multiple: false
});
})

file_frame.on('select', function () {
.on('select', function () {
attachment = file_frame.state().get('selection').first().toJSON();
self.prev('.wpsa-url').val(attachment.url).change();
});
})

// Finally, open the modal
.open();
});

$('.wpsa-image-browse').on('click', function (event) {
event.preventDefault();
var self = $(this);

// Create the media frame.
var file_frame = wp.media.frames.file_frame = wp.media({
title: self.data('uploader_title'),
button: {
text: self.data('uploader_button_text'),
},
multiple: false,
library: { type: 'image' }
})

.on('select', function () {
attachment = file_frame.state().get('selection').first().toJSON();
var url;
if (attachment.sizes && attachment.sizes.thumbnail)
url = attachment.sizes.thumbnail.url;
else
url = attachment.url;
self.parent().children('.wpsa-image-id').val(attachment.id).change();
self.parent().children('.wpsa-image-preview').children('img').attr('src', url);
})

// Finally, open the modal
file_frame.open();
.open();
});
});
</script>
Expand Down