Script for importing data to WordPress

Published on  by Gearloose ‐ In  ‐ Reading time: less than a minute

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 = '';
$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 = '';
$blog_name = 'Sitename';
$blog_desc = 'Site Descr';

//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'];

//Done Categories

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


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=""

    <title>' . $blog_name . '</title>
    <link>' . $blog_url . '</link>
    <description>' . $blog_desc . '</description>
    <pubDate>' . date("D, d M Y G:i:s O", time()) . '</pubDate>
    <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) . '


Related Posts

How to Set Up Multiple Blogs Using a Single CMS
Ethereum mining for Windows 7/10 - How To
How to connect your ps4 controller to your android mobile
How to build a Raspberry Pi Entertainment System in NES case