Script for importing data to WordPress

When I wanted to move my custom CMSto WordPress, I couldn’t find a proper method to do it. If you are dealing with the same problem, see something very easy to do. All you need is a word processor, although you might want to use something like Notepad ++ or another editor (eg Dreamweaver).

Note: This code is in a simple format to make it easy to understand. Although it works, I do not take any responsibility for any error or deletion of your database. Use backup before doing anything.

Create a new php file and named it something like import.php. Before run it, you should put the login information to the database for your old CMS.

The script will create an xml file to import it with WP.


$dbhost = 'www.mysql.com';
$dbusername = 'username';
$dbpasswd = 'pass';
$database_name = 'dbname';
$connection = @mysql_connect("$dbhost", "$dbusername", "$dbpasswd")
or die("Couldn't connect to server.");
$db = @mysql_select_db("$database_name", $connection)
or die("Couldn't select database.");
mysql_query("SET NAMES 'utf8'");

Now we start to keep posts, comments and categories in arrays. We can add an option to move the members too, but it is not necessary.


$blog_url = 'http://www.site.com';
$blog_name = 'Sitename';
$blog_desc = 'Site Descr';

//Categories
//Let's say we have categories
$sql = mysql_query("select * from site_categories order by id")
or die(mysql_error());
$cats = array();
while ($row = mysql_fetch_array($sql, MYSQL_ASSOC))
$cats[] = $row['title'];

unset($row);
mysql_free_result($sql);
//Done Categories

//Posts
//Let's say we have posts within categories
$sql = mysql_query("select site_posts.*, site_categories.title as ctitle,
site_users.username as uname, site_users.id as uid
from site_posts
LEFT JOIN site_categories on site_categories.id = site_guides.catid
LEFT JOIN site_users on site_users.id = site_guides.poster
order by site_posts.id ASC") or die(mysql_error());
$posts = array();
while ($row = mysql_fetch_array($sql, MYSQL_ASSOC))
$posts[] = $row;

unset($row);
mysql_free_result($sql);
//Done

So we’re ready with the basics. Let’s start building the xml file.


$xml = '<!-- XML file header from wordpress export, just copy and replace values when necessary -->

<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
<!-- It contains information about your blog\'s posts, comments, and categories. -->
<!-- You may use this file to transfer that content from one site to another. -->
<!-- This file is not intended to serve as a complete backup of your blog. -->

<!-- To import this information into a WordPress blog follow these steps. -->
<!-- 1. Log into that blog as an administrator. -->
<!-- 2. Go to Tools: Import in the blog\'s admin panels (or Manage: Import in older versions of WordPress). -->
<!-- 3. Choose "WordPress" from the list. -->
<!-- 4. Upload this file using the form provided on that page. -->
<!-- 5. You will first be asked to map the authors in this export file to users -->
<!-- on the blog. For each author, you may choose to map to an -->
<!-- existing user on the blog or to create a new user -->
<!-- 6. WordPress will then import each of the posts, comments, and categories -->
<!-- contained in this file into your blog -->

<!-- generator="WordPress/3.0.1" created="' . date("Y-m-d G:i", time()) .
'"-->
<rss version="2.0" xmlns:excerpt="http://wordpress.org/export/1.0/excerpt/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:wp="http://wordpress.org/export/1.0/"
>

<title>' . $blog_name . '</title>
<link>' . $blog_url . '</link>
<description>' . $blog_desc . '</description>
<pubDate>' . date("D, d M Y G:i:s O", time()) . '</pubDate>
<generator>http://wordpress.org/?v=3.0</generator>
<language>en</language>
<wp:wxr_version>1.0</wp:wxr_version>
<wp:base_site_url>' . $blog_url . '</wp:base_site_url>
<wp:base_blog_url>' . $blog_url . '</wp:base_blog_url>

<!-- END of XML file header -->';


//First the categories
foreach ($cats as $cat)
$xml .= '
<wp:category>
<wp:category_nicename>' . seo_slugs($cat) . '</wp:category_nicename>
<wp:category_parent></wp:category_parent>
<wp:cat_name>
</wp:category>';
//OK

unset($cats); //Free memory

Now the posts:


foreach ($posts as $key => $row) {
//We want posts to have this url: το www.site.com/2010/04/title
//So:
$post_year = date("Y", $postd);
$post_month = date("m", $postd);

$title = $row['title'];
$link = $blog_url . '/' . $post_year . '/' . $post_month . '/' . seo_slugs($title) .
'/';
$pubdate = date("D, d M Y H:i:s \G\M\T", $postd);
$guid = $blog_url . "/?p=" . $row['id'];
$body = $row['introtext'];
$body.= '<!--more-->';
$body.= $row['body'];
$body = stripslashes($body); //If the addslashes() was used

$xml .= '
<item>
<title>' . $title . '</title>
<link>' . $link . '</link>
<pubDate>' . $pubdate . '</pubDate>
<dc:creator>
<category>
<category domain="category" nicename="' . seo_slugs($row['ctitle']) .
'">
<guid isPermaLink="false">' . $guid . '</guid>
<description>
<content:encoded>
<excerpt:encoded>
<wp:post_id>' . $row['id'] . '</wp:post_id>
<wp:post_date>' . date('Y-m-d H:m:s', $postd) . '</wp:post_date>
<wp:post_date_gmt>' . date('Y-m-d H:m:s', $postd) . '
<wp:comment_status>open</wp:comment_status>
<wp:ping_status>open</wp:ping_status>
<wp:post_name>' . seo_slugs($title) . '</wp:post_name>
<wp:status>publish</wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type>post</wp:post_type>
<wp:post_password></wp:post_password>
<wp:is_sticky>0</wp:is_sticky>
<wp:postmeta>
<wp:meta_key>views</wp:meta_key>

</wp:postmeta>';

If we have any comments, here is the right place to put them. However, it is not necessary for the blog to work.


$sql = mysql_query("select site_comments.*, site_users.username as uname, site_users.usernameseo as useo, site_users.email as umail
from site_comments
LEFT JOIN site_users on site_users.uid = site_comments.user_id
where site_comments.comment_post_ID = '". $row['id']."'
ORDER BY comment_date ASC") or die(mysql_error());
if (mysql_num_rows($sqlc) > 0) {
while ($comm = mysql_fetch_assoc($sqlc))
{
$xml .= '
<wp:comment>
<wp:comment_id>' . $comm['comment_ID'] . '</wp:comment_id>
<wp:comment_author>
<wp:comment_author_email>' . $comm['umail'] . '</wp:comment_author_email>
<wp:comment_author_url></wp:comment_author_url>
<wp:comment_author_IP>' . $comm['comment_author_IP'] . '</wp:comment_author_IP>
<wp:comment_date>' . date('Y-m-d H:m:s', $comm['comment_date']) . '</wp:comment_date>
<wp:comment_date_gmt>' . date('Y-m-d H:m:s', $comm['comment_date']) . '</wp:comment_date_gmt>
<wp:comment_content>
<wp:comment_approved>1</wp:comment_approved>
<wp:comment_type></wp:comment_type>
<wp:comment_parent>0</wp:comment_parent>
<wp:comment_user_id>' . $comm['user_id'] . '</wp:comment_user_id>
</wp:comment>';
}
mysql_free_result($sqlc);
}

$xml .= '</item>';
}

That’s it. In the $xml variable we have all the data we need. Now we need to write it down in a file that suits WP.


$fp = fopen('access.xml', 'w');
fwrite($fp, $xml);
fclose($fp);

The “seo_slugs” function:

function seo_slugs($item)
{
$item = preg_replace('/[^a-zA-Z0-9 *]/', '', $item);
$item = str_replace('/', '', $item);
$item = str_replace(' ', '-', $item);
$item = str_replace('.', '-', $item);
$item = strtolower($item);
return $item;
}

Leave a Reply