Using WordPress Media Uploader in Plugin or Theme

WordPress media Uploader is very Good which can also be used in your Themes or Plugins for uploading logo’s or anything else, WordPress Media Uploader makes it easy to upload files easily by the Users that is a plus point

So now lets begun, it is Easy to implement the WordPress Media uploader, first of all prepare an Piece of HTML like the below one :

<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 we are going to add some scripts so add this below code :

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');
}

in the above code replace my_plugin_page with your page string for example

http://localhost/wp-admin/themes.php?page=theme-options

So in the above line you can see the String theme-options is the string

Now here comes the my-script.js code :

jQuery(document).ready(function() {

jQuery('#upload_image_button').click(function() {
 formfield = jQuery('#upload_image').attr('name');
 tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
 return false;
});

window.send_to_editor = function(html) {
 imgurl = jQuery('img',html).attr('src');
 jQuery('#upload_image').val(imgurl);
 tb_remove();
}

});

Now you have Successfully implemented WordPress Media Uploader in your Theme or Plugin

Thank you Matt for this Trick

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>