University Marketing & Communications

June 14, 2012

Write a custom WordPress Importer, part 1


This was originally posted on Chris Heiland’s staff blog while he was a member of the Web Team. Chris migrated this post to the Web Team blog before he left because we thought the content still had value to the UW community.

Moving content from one system to another is always a conversation, many times it makes sense to rewrite and think about each new article. The goal should always be cleanup and enhance the experience for the user. However, in some cases it makes sense just to automatically script the process for expediency or when content doesn’t need to be modified.

There are different ways to automatically import content into WordPress including several content importers to deal with a number of cases. I had about 10,000 articles stored in a sqlite3 database that I needed to make available in WordPress. The easiest way for this was to do a direct import rather than mess with a text file to transition between the two.

I started researching how to write a custom WordPress Importer but I couldn’t find much in the way of documentation.This is the first post in a series on how to take content and write a custom importer to properly migrate the content into WordPress. There isn’t much to it, but there are a few tricks to make it easier.

For my process I started looking at a few importer plugins to see what an implementation would look like. I spent the most time looking at the wordpress importer as it was written by the wordpressdotorg team. There are some basic elements that take it from a standard plugin to a custom importer.

The first step is to inherit from the WP_Importer class and import the files to do so. Basically your first few lines of your importer will look like this:

Plugin Name: {{ Plugin Name }}
Plugin URI: {{ Plugin URI }}
Description: {{ Plugin Description }}
Author: {{ Author}}
Author URI: {{ Author URI }}
Version: {{ Version }}
Text Domain: {{ Importer Name }}-importer
License: GPL version 2 or later -

if ( !defined('WP_LOAD_IMPORTERS') )

/** Display verbose errors */
define( 'IMPORT_DEBUG', true );

// Load Importer API
require_once ABSPATH . 'wp-admin/includes/import.php';

if ( ! class_exists( 'WP_Importer' ) ) {
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
if ( file_exists( $class_wp_importer ) )
require $class_wp_importer;

* {{ Custom Importer Name }} Importer {{ Description }}
* @package WordPress
* @subpackage Importer
if ( class_exists( 'WP_Importer' ) ) {
class {{ Custom Importer Name }}_Import extends WP_Importer {

  var $authors = array();
  var $posts = array();
  var $terms = array();
  var $categories = array();
  var $tags = array();
  var $base_url = '';

  function {{ Custom Importer Name }}_Import() { /* nothing */ }

  // Code that actually handles the import


The bulk of the code should be self explanatory. The main things to look out for are any of the variables marked with {{ variables }}. Replace the generic names with something specific to what you are importing. The class variables are just a starting point, depending on what you have to import you may use some or all of these.

There is quite a bit of flexibility on what you are able to do. The work is determined by what the source content looks like and how much you want to preserve. There are a few more steps to make a fully functioning importer, but this should get you started.

Here is part 2 for the next step in migrating content into WordPress.