Script for importing data to WordPress

July 31, 2010 03:32:06

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 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 .= '


' . seo_slugs($cat) . '