Using the WordPress Uploader in Your Plugin or Theme
January 8th, 2010 by MattWordPress has a nice media uploader dialog that it uses on the editor pages. Now wouldn’t it be nice if you could use it to handle image uploads for part of a plugin or theme you’re writing? Maybe you want to add an easy way to change the logo in a theme? A simple “Upload Image” button would work quite well for that, wouldn’t it?

It’s fairly simple to implement, providing you already have a bit of experience with the WordPress API.
The first step is to prepare your HTML. Put it wherever the code for your admin page is. You want to have a text input for the image URL, and a button that will launch the uploader dialog.
<tr valign="top"> <th scope="row">Upload Image</th> <td><label for="upload_image"> <input id="upload_image" type="text" size="36" name="upload_image" value="" /> <input id="upload_image_button" type="button" value="Upload Image" /> <br />Enter an URL or upload an image for the banner. </label></td> </tr>
Now that the easy part is out of the way, it’s time to start making it do something. You need to enqueue some scripts and styles. Here’s an example function to show how it’s done:
function my_admin_scripts() {
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_register_script('my-upload', WP_PLUGIN_URL.'/my-script.js', array('jquery','media-upload','thickbox'));
wp_enqueue_script('my-upload');
}
function my_admin_styles() {
wp_enqueue_style('thickbox');
}
if (isset($_GET['page']) && $_GET['page'] == 'my_plugin_page') {
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');
}
We need the media-upload and thickbox scripts for starters, as well as jQuery, which is already included. Then we have to register and enqueue our own JavaScript file, my-script.js, which will handle the media uploader functionality. We also need to load the thickbox stylesheet in the next function.
The if (…) block ensures that the scripts and styles will only be included if the user is on a specific admin page. If you look at your plugin’s (or theme’s) admin page, the URL should have a ?page=some_string at the end. Substitute my_plugin_page for that string.
Now for the part the actually invokes the uploader: the JavaScript. This will go in the my-script.js file we included earlier.
jQuery(document).ready(function() {
jQuery('#upload_image_button').click(function() {
formfield = jQuery('#upload_image').attr('name');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery('#upload_image').val(imgurl);
tb_remove();
}
});
The first click() event listener opens a ThickBox dialog when the “Upload Image” button is clicked, and loads the uploader page inside it. It also stores the name of the URL input field in a variable, for later use.
The second function overrides the send_to_editor() function in the media-upload script. This is probably the most important part. When the “Insert into Post” button is clicked in the uploader dialog, this function fires. It collects the URL of the image that was uploaded, dumps it into the awaiting form field, and closes the ThickBox dialog.
That’s it. Providing everything went according to planned, you should have a form field that will either accept an arbitrary image URL, or allow a user to upload one on the spot.

Very cool – It looks like Wordpress theme creators are spending more and more time on the details, and of course the ability to easily swap out the logo in a theme is a great little feature.
Don’t forget plugin creators! I put this post together because I was working on a plugin. I figured out how to do it by studying a couple of plugins that had upload features.
May have to test drive this into my Custom Login theme.
Thanks for the article. It really got me thinking
May sound like a stupid request, but could you provide a ‘generic’ plugin with nothing more than the input box in the admin page, along with the way I would call that image in my template?
I was thinking of trying this idea to allow my user to upload the background image for their theme as a starter, and if I can see how that works, then I’m pretty sure I can hack the code to use it in other places.
I don’t really want to be maintaining another plugin at this point.
However, the code I provided above is fairly nonspecific, and should be possible to fit into any application, giving some work. It would require some PHP/WordPress experience though.
However, if you’re looking for a quicker solution, you might try looking at existing themes or plugins with uploader capabilities. I think Kubrick, the default WP theme, might have a similar uploader (?) to allow users to add a background image to the header. I may not be remembering correctly about Kubrick, but I know there are several themes that offer functionality along those lines. I should be trivial to borrow some code from the themes and insert it into your functions.php.
Wow, that’s great. Thank you! Will use it for a Client Website, so he can change the Logo easily.
I managed to get this working on my site, but I have one quick question if you don’t mind.
How do I change it from a single image selection to an array of selections?
Meaning, my theme has a logo, page background, and a footer background. I would like to have a page with 3 uploaders so that the user can select an image for each field.
I tried replicated the code 3x on the same page, but that just threw up a ton of errors.
Another reader worked out a way to have multiple uploaders: http://www.theenglishguy.co.uk.....tions-php/
Just make sure you give the input tags different name and id attributes.
This is so great . I was looking for that
How can I use it in Theme option or custom meta box in post ?
hope to found the answer .
Thanks alot
Sub
You can use it wherever you want, providing you queue-up the requisite scripts. So, yes, it would work fine in either a meta box or an options page.
Awsome thanks! With these guidelines I could quite easily implement this.
First I tried getting some other jquery uploader and image crop tool working, but when the WP supplied tools work – and is easily implementable like you guided – then imho its clearly best to use them.
How can this be done with a custom meta box and not screw up the regular media uploader? Right not it will automatically pull anything you try to use in the post even if your not trying ot use the custom meta box/field.
It should depend on what ids you use for the form and script.
Mind providing an example that would work? I basically just used your example, which didnt as it obviously conflicted like i mentioned before. Thanks for the help. I appreciate it.
I looked over the code again, and I think your problem might be because the script overrides the window.send_to_editor function from the uploader. My JS skills aren’t really sufficient to prescribe a solution for your scenario, but you might want to look into something like this:
http://stackoverflow.com/quest.....e-original
I’ve come up with this code to allow for the original send to editor to work.
jQuery(document).ready(function() {
var formfield;
jQuery(‘#Image_button’).click(function() {
jQuery(‘html’).addClass(‘Image’);
formfield = jQuery(‘#Image’).attr(‘name’);
tb_show(”, ‘media-upload.php?type=image&TB_iframe=true’);
return false;
});
/*
window.send_to_editor = function(html) {
if ( jQuery(‘html’).hasClass(‘Image’) ) {
imgurl = jQuery(‘img’,html).attr(’src’);
jQuery(‘#Image’).val(imgurl);
tb_remove();
jQuery(‘html’).removeClass(‘Image’);
}
}*/
// user inserts file into post. only run custom if user started process using the above process
// window.send_to_editor(html) is how wp would normally handle the received data
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function(html){
if (formfield) {
fileurl = jQuery(‘img’,html).attr(’src’);
jQuery(‘#Image’).val(fileurl);
tb_remove();
jQuery(‘html’).removeClass(‘Image’);
} else {
window.original_send_to_editor(html);
}
};
});
Which I modified from the code snippet
Matt!!!
Thank you very much, dude. Your tips was extremely helpful for me to implement my “featured” plugin. I searched all over the internet for this, and thanks Godgle I found your post.
Now my little plugin is just rocking.
Thanks again!