Skip to content

Instantly share code, notes, and snippets.

@pgooch
Created October 20, 2012 21:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pgooch/3924890 to your computer and use it in GitHub Desktop.
Save pgooch/3924890 to your computer and use it in GitHub Desktop.
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