Skip to content

Commit f7e3ddf

Browse files
committed
wp media uploader added
1 parent 96f4373 commit f7e3ddf

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

class.settings-api.php

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,10 @@ public function __construct() {
4242
*/
4343
function admin_enqueue_scripts() {
4444
wp_enqueue_style( 'wp-color-picker' );
45-
wp_enqueue_style( 'thickbox' );
4645

46+
wp_enqueue_media();
4747
wp_enqueue_script( 'wp-color-picker' );
4848
wp_enqueue_script( 'jquery' );
49-
wp_enqueue_script( 'media-upload' );
50-
wp_enqueue_script( 'thickbox' );
5149
}
5250

5351
/**
@@ -293,27 +291,10 @@ function callback_file( $args ) {
293291
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
294292
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
295293
$id = $args['section'] . '[' . $args['id'] . ']';
296-
$js_id = $args['section'] . '\\\\[' . $args['id'] . '\\\\]';
297-
$html = sprintf( '<input type="text" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
298-
$html .= '<input type="button" class="button wpsf-browse" id="'. $id .'_button" value="Browse" />
299-
<script type="text/javascript">
300-
jQuery(document).ready(function($){
301-
$("#'. $js_id .'_button").click(function() {
302-
tb_show("", "media-upload.php?post_id=0&amp;type=image&amp;TB_iframe=true");
303-
window.original_send_to_editor = window.send_to_editor;
304-
window.send_to_editor = function(html) {
305-
var url = $(html).attr(\'href\');
306-
if ( !url ) {
307-
url = $(html).attr(\'src\');
308-
};
309-
$("#'. $js_id .'").val(url);
310-
tb_remove();
311-
window.send_to_editor = window.original_send_to_editor;
312-
};
313-
return false;
314-
});
315-
});
316-
</script>';
294+
295+
$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 );
296+
$html .= '<input type="button" class="button wpsa-browse" value="'.__( 'Browse' ).'" />';
297+
317298
$html .= sprintf( '<span class="description"> %s</span>', $args['desc'] );
318299

319300
echo $html;
@@ -472,6 +453,7 @@ function script() {
472453
jQuery(document).ready(function($) {
473454
//Initiate Color Picker
474455
$('.wp-color-picker-field').wpColorPicker();
456+
475457
// Switches option sections
476458
$('.group').hide();
477459
var activetab = '';
@@ -511,7 +493,38 @@ function(){
511493
$(clicked_group).fadeIn();
512494
evt.preventDefault();
513495
});
514-
});
496+
497+
var file_frame = null;
498+
$('.wpsa-browse').on('click', function (event) {
499+
event.preventDefault();
500+
501+
var self = $(this);
502+
503+
// If the media frame already exists, reopen it.
504+
if ( file_frame ) {
505+
file_frame.open();
506+
return false;
507+
}
508+
509+
// Create the media frame.
510+
file_frame = wp.media.frames.file_frame = wp.media({
511+
title: self.data('uploader_title'),
512+
button: {
513+
text: self.data('uploader_button_text'),
514+
},
515+
multiple: false
516+
});
517+
518+
file_frame.on('select', function () {
519+
attachment = file_frame.state().get('selection').first().toJSON();
520+
521+
self.prev('.wpsa-url').val(attachment.url);
522+
});
523+
524+
// Finally, open the modal
525+
file_frame.open();
526+
});
527+
});
515528
</script>
516529

517530
<style type="text/css">

0 commit comments

Comments
 (0)