Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. Plugins

Custom Database Tables: Importing Data

This post is part of a series called Custom Database Tables.
Custom Database Tables: Exporting Data

In the last tutorial we looked at exporting data from a custom table. Of course this is only half the story - we obviously need to provide a way of importing that data. Naturally, WordPress doesn't handle this - so once more we need to roll our own.

In the previous article in this series we noted that if our data contains references to one of the native WordPress tables (a post ID for example) then we rather quickly hit difficulties. The reason is that when importing such data, the post ID being referenced may not exist and if it does, may not be the correct ID. This is because when posts are imported, their ID may change to avoid collisions in the database (IDs must be unique).

Normally this is ok: linked data is imported together and the references are updated during the import routine to ensure that any changes propagate throughout the entire dataset. However, as discussed in the previous article - it's actually very difficult (except in particular cases) to import our custom data alongside the native data. So the caveats mentioned in that article carry forward to this one - and as before, although the example below references WordPress tables, it is used simply for consistency with the rest of the series.

The Mark-Up

We want to allow our users to import data from the export file generated in the last article in this series - so let's start with adding a form that allows the user to upload that file. We'll do this by modifying the class we defined in last tutorial.

Above we've added the following methods

  • maybe_upload() - which will act as a listener for a file being submitted for importing.
  • admin_notices() - which will display a success or error notice after attempting to import a file.
  • import() - which will receive an uploaded file and import the data.
  • parse() - a helper function called by import() to parse the uploaded file, and extract the logs it contains.

But first, we'll add a form by which we can upload a file. We'll add this below the export button we created in the previous article. To do this we'll need make some alterations to the display() method, responsible for producing the mark-up of our admin page. Since this second form will submit a file, we need to set the encoding type to 'multipart/form-data'.

Handling the Form Submission

Next, we want to listen for when the above form is submitted and trigger the import routine. Before doing that it's important to run a few checks:

  • Does the user have permission to upload files? Here we've granted only those who can manage_options the ability to upload.
  • Did the user intend to upload the files (we check this by verify the nonce)
  • Was a file actually uploaded? And was it of the correct type
  • Were there any errors in the upload?

Optionally you can place a limit on the size of the uploaded file as a sort of 'sanity check'. In this example I've capped it at 2MB. (A useful function for formatting file sizes in a 'human readable way' is the function size_format).

Importing the File

Next we need to import the file. First we'll need to extract the logs from the uploaded file - and we'll delegate that job to the parse() method (we'll get to that method in a bit).

Once we have these logs we'll first check if they already exist (to avoid any accidental duplication), before inserting them. When checking the logs we'll just check the user ID and the activity date. We could, if we wanted, be more strict (checking activity, object ID and type etc.) but we would need to go back and extend our API (specifically wptuts_get_logs()).

Once we've imported the logs we redirect the user back to our admin page. We'll add a query variable to the URL (imported) to store how many logs were imported (if any). This way we can display an appropriate admin message.

Parsing the File

We still need to define the parse() method which, given the uploaded file, should extract the data and return it as an array of logs. Fortunately, with PHP's built-in XML handler this is a fairly simple task.

Displaying the Admin Notice

Finally, we want to define our admin_notices() method to display an appropriate message after the file has been uploaded. Recall that once the import routine has finished, we redirect the user back to our admin page, with the query variable imported added, storing the number of imported logs. We use this to determine whether we should display an error or success message.

We also check the screen ID so that we only display the notice on our admin page. If you're not sure what the screen ID of your admin page is, see this article.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.