WordPress Using Media uploader in your plugin

Hi Guys,

I have to agree that with wordpress, possibilities are infinite (may be). As I am still learning WordPress API and still have to master the art of Hooks and Crooks (I mean filters) :) ,

I had developed very basic plugins for my couple of website but they were really basic. So everyday with WordPress I am learning something new. Last week I learned how can I use the available Media Uploader that ships with WordPress. So rather to reinvent the wheel, you can use whatever is already available

Ok so lets get started.

First You will need couple of includes so in you admin_init action or whichever hook you are using to enqueue the scripts put this code there

  1. wp_enqueue_script('thickbox');
  2. wp_enqueue_script('media-upload');
  3. wp_enqueue_style('thickbox');

This makes sure that thickbox (modaled window) and media-upload scripts are available. As you notice that we’ve imported thinkbox style too. WordPress knows where these scripts are so you don’t have to worry about the paths here.

Ok So this is done Now we will create a very basic form as shown below. This form will be on one of your Admin pages.

  1. <table class="widefat">
  2. <tr valign="top">
  3. <th scope="row">Upload CSV</th>
  4. <td><label for="upload_image"></label>
  5. <input id="upload-csv" type="text" size="36&#8243; name="upload_csv" value="" />
  6. <input id="upload-csv-button" type="button" class="button-primary" value="Upload csv" />
  7. <br />Upload your CSV file..</strong>
  8. </td>
  9. </tr>

Advertisement

 

Now that our HTML Form has been setup lets do some jQuery magic to open media uploader page. you can place this script just under your form or you can include it in your JS file. because we are using document.ready (pseudo) so the script wont execute until the page get loaded completely.

  1. <script type="text/javascript">
  2. jQuery(document).ready(function() {
  3. jQuery('#upload-csv-button').click(function() { // bind click event to out form button
  4. tb_show('', 'media-upload.php?type=file&TB_iframe=true'); // type= can be image too, browse codex for another option
  5. return false;
  6. }); // this will open media uploader
  7. window.send_to_editor = function(html) { // when insert into post is clicked
  8. var a =jQuery(html); // we get path to file
  9. fileurl = a.attr('href'); // if you are using type=image then use 'src' instead of 'href'
  10. jQuery('#upload-csv').val(fileurl);
  11. tb_remove();
  12. // do you other AJAX magic here
  13. }
  14. });
  15. </script>

Ok so this way we will have our file or image uploaded and have path of the Image which we can store in database or do other manipulations.

 

I hope this Tip will help you.

I will be writing on how to add custom buttons on top of content editor soon so stay tuned

Till then Adios!

 

Advertisement

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Comments

  1. This is really good help. thanks

  2. Thanks Jaspreet… finally a good explanation to implement the media uploader in my custom plugin!
    Thanks again, now I can start my small project :)
    Roberto Iacono recently posted..Barra fissa espandibile in WordPress: jQuery Expandable Sticky BarMy Profile

  3. Thanks Jaspreet… It’s indeed a helpful one..And yes, with wordpress we are bound to learn something new everyday as it is with any open source mediums, the more we use, the more it expands and in-turn the more we learn..:)

Speak Your Mind

*

CommentLuv badge