Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Use the default wordpress media uploader into your plugin.
<!-- This is just the code for the button and hidden input -->
<input id="backdrop_upload_button" value="Upload/Select Image" type="button" class="button" />
<input type="hidden" name="the_image" id="the_image" value="<?= $settings['the_image'] ?>" />
// This is the jQuery that opens the uploader and gets the result, then places it in the hidden input
var $ = jQuery;
// Handels image uploading
$(document).ready(function(){
$("input#upload_button").click(function(){
tb_show('','media-upload.php?type=image&post_id=1&TB_iframe=true&flash=0&backdrop=true');
return false;
})
window.send_to_editor=function(html){
var img = $('<div>'+html+'</div>').find('img').attr('src');
$('#the_image').val(img);
tb_remove();
};
});
// This adds the required scripts to the admin head
add_action('admin_print_scripts',array(&$this,'uploader_scripts'));
add_action('admin_print_styles',array(&$this,'uploader_styles'));
public function uploader_scripts(){
wp_enqueue_script('media-upload');
wp_enqueue_script('thinkbox');
}
public function uploader_styles(){
wp_enqueue_style('thickbox');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment