In this tutorial I’ll explain how to develop a basic plugin that will allow you to upload files directly from your computer to WordPress. I won’t go into the basic details on how to create a plugin, WordPress already did that in their Codex. This plugin can be used as a basic structure for an import plugin, for saving specific data (csv, Images…etc) to database or extract and handle imported data with specific functions you create. You know, the sky’s the limit ;).

Step 1: Add an admin menu for the upload form.

First thing we’ll do is adding the plugin into the menu so we can access it via WordPress administration. Following function will do the trick:

function wpali_setup_import_menu(){
add_menu_page( 'Import location Page', 'Import location Plugin', 'manage_options', 'upload-plugin', 'wpali_upload' );
add_action('admin_menu', 'wpali_setup_import_menu');

If you are not familiar with the code above, check here.

Step 2: Displaying the upload form.

Now that we have added our plugin to WordPress dashboard, we need to create a function to call the content of your plugin. Name of this function must match the function specified earlier in add_menu_page().

function wpali_upload(){
<h1>Import function content</h1>
<h2>Upload a File</h2>
<form method="post" enctype="multipart/form-data">
<input type='file' id='file' name='file'></input>
<input type="submit" name="submit" value="Submit" />

In the code above, we did 2 things. We made a call to to another function wpali_upload_handle_post() which we will create later to handle post request, and we added an html form with input type file that will be displayed as the content of our admin menu.

Step 3: Handle post request

We need to create the function that we called earlier, and check if a file was uploaded:

function wpali_upload_handle_post(){
if (!empty($_FILES['file']['tmp_name'])) {

echo "file is not empty";


The code above will show a message based on whether the file field is empty or not. Now based on the result we want to trigger an action, if a file was actually submitted we want to upload to WordPress  with wp_handle_upload() function, and show a link, We can do this by replacing :

echo "File is not empty";

With this:

require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['file'];
$upload_overrides = array('test_form' => FALSE);
$file = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $file && ! isset( $file['error'] ) ) {
echo "<h3>File uploaded successfully</h3>";
$filename = $file['url'];
echo $filename;
} else {

echo $file['error'];

The code above is explained in Codex.


That’s it for this tutorial, it is by no mean a complete plugin, you still need to take care of validation and file types…etc; and add you own content to the last function. you can find the full code here on github.