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

wp_enqueue_script(‘thickbox’);
wp_enqueue_script(‘media-upload’);
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.

[geshi lang=”html5″ nums=”1″ target=”_self” ]

Upload CSV



Upload your CSV file..

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.

[geshi lang=”javascript” nums=”1″ target=”_self” ]

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. Venkatc
    Twitter:
    says:

    Excellent innovation to people very good plugin still trying to learn it.

  2. Venkatc
    Twitter:
    says:

    Good information , nice content very useful all of us, thank u.

  3. This is really good help. thanks

  4. Thanks Jaspreet… finally a good explanation to implement the media uploader in my custom plugin!
    Thanks again, now I can start my small project :)

  5. 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