Creating a WordPress Plugin Part 2: Uploading Media and using Web Services

In my previous article I took a look at getting a basic admin panel to appear in the sample application. In this article I will take a look at linking a web service to the plugin so that we can convert PDFs to HTML. But first, we need to upload PDFs. The web service I’ll be using is our own PDF to HTML converter.

Uploading Files

The goal of the plugin is to convert a PDF in WordPress’ media directory into HTML using the web service. So first of all we need to extend the plugin we created last time to have a form which handles the uploads. We also need to handle the incoming file, which we do in test_handle_post(), where we use the media_handle_upload() function to upload the file to the media folder. Below is the commented code of how to do so:

The admin page should now look like this when a file has been uploaded:

upload

And to verify it went into the media folder:pdf

Now we want to actually convert the file to html using our web service. Below I have amended the conversion method onto the plugin (Note: using the converter requires a username and password – to sign up for a free trial click here):

And if we look in the output directory of the plugin folder, we should see our converted document:

output

 

Now that we know how to upload a PDF file to the media directory, it is also possible to create list of the files to display and then select which one to use. I won’t mention how to do it here for the sake of brevity but if you’re interested in trying this, have a look at get_posts() method on the WordPress Codex.

Next time: embedding our content using shortcodes.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the  Blog RSS.

Related Posts:

The following two tabs change content below.
Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.
Simon

About Simon Lissack

Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.

One thought on “Creating a WordPress Plugin Part 2: Uploading Media and using Web Services

  1. bhawna

    Hey nice and useful post . I want to know how to get the url of the uploaded image file in first plugin?

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=""> <s> <strike> <strong>