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 = '
' . $blog_name . '
' . $blog_url . '
' . $blog_desc . '
' . date("D, d M Y G:i:s O", time()) . '
http://wordpress.org/?v=3.0
en
1.0
' . $blog_url . '
' . $blog_url . '
';
//First the categories
foreach ($cats as $cat)
$xml .= '
//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 GMT", $postd);
$guid = $blog_url . "/?p=" . $row['id'];
$body = $row['introtext'];
$body.= '';
$body.= $row['body'];
$body = stripslashes($body); //If the addslashes() was used
$xml .= '
-
' . $title . '
' . $link . '
' . $pubdate . '
' . $guid . '
' . $row['id'] . '
' . date('Y-m-d H:m:s', $postd) . '
' . date('Y-m-d H:m:s', $postd) . '
open
open
' . seo_slugs($title) . '
publish
0
0
post
0
views
';
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 .= '
}
mysql_free_result($sqlc);
}
$xml .= '';
}
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;
}