Importing old content into WordPress

Moving to WordPress as the CMS has been one of the best decisions I’ve made. However it left me with a lot of content that were not in the WordPress database. Then there were the task of importing…

I have been testing out many WordPress plugins that supposedly should do the job easily. They mostly work by importing from csv or xml files which are easily enough created from a database. However I was not able to find a plugin that actually worked perfectly. No matter what I did some characters would end up as question marks or black diamonds. I do understand this is a result of  character sets in use. But forcing a setting for these did not help either.

That’s when I decided to write my own code to import old material. Something which required a bit if digging,  but in the end was very simple once I had everything figured out. The key to fixing the problem with question marks and black diamonds in the text was that this actually seem to happen when WordPress inserts a new post. The solution was to add a line of code that prevented WordPress from applying its filters:

remove_all_filters(“content_save_pre”);

Here is what I did:

// first connect to your old database
$db = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db("$dbname",$db);

$sql = 'YOUR SELECT STATEMENT FOR FETCHING OLD CONTENT';

// refer to the WordPress file so that you can integrate wit WordPress
require('./wp-blog-header.php');

// IMORTANT, this is what stops WordPress from messing up the content when you import it
remove_all_filters("content_save_pre");

mysql_query("SET NAMES 'utf8'", $db);
$queryResource = mysql_query($sql, $db);

$iCount = 1;

// run a loop for all the records in your old database that you want to import (this will obviously depend a bit on what content you want to import)
while ($row = mysql_fetch_array($queryResource, MYSQL_ASSOC)) {
  $sID = $row['id'];
  $m_body = $row['body'];
  $m_title = $row['title'];
  $m_date = $row['date'];

  $sDate = $m_date . ' 12:00:00';
  $postdate = date($sDate);

  // Create post object that you will use to insert the new post into WordPress

  // This part is used for the unique slug, which makes it easier to create a php based 301 redirect script from the old content if you want to do that.
  $sPostName = 'new_permalink'.$sID;

  // post_author is the id of the user the new post should be connected to in WordPress
  // post_category is the id of the category in WordPress
  $my_post = array(
    'post_title' => wp_strip_all_tags($m_title),
    'post_content' => $m_body,
    'post_date' => $postdate,
    'post_name' => $sPostName,
    'post_status' => 'publish',
    'post_author' => 16,
    'post_category' => array(3)
    );

  // Insert the post into the database
  wp_insert_post( $my_post );
  }

mysql_close($db);

Share This Post