I have created a new website based on WordPress, to replace an old custom script site, and wanted to import comments from the old site into the new wordpress site.
I have rewritten articles in the new wordpress site, but I had to find a way to import only the comments.
I have tried exporting an article with a comment, then manually modify the WXR export file to leave only sufficient information for the comment to import correctly. Many tries with import errors thrown by WordPress Importer, like Invalid post “”, and so on.
After searching for a working solution a little bit more, I found a little help on importing comments here: http://shibashake.com/wordpress-theme/wordpress-xml-import-format-comments.
The file described was not working on my WordPress 3.4.2 install, but managed to modify my file in a similar way with his, and now the only message thrown by WordPress Import was “Post already exists” – “All done. Have fun!”
When I looked into the Comments section, the imported comment was there, and already shown in the post page too. Perfect!
The header of the file looks like this:
<?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0" xmlns:excerpt="http://wordpress.org/export/1.2/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.2/" > <channel> <language>en-US</language> <wp:wxr_version>1.2</wp:wxr_version> <generator>http://wordpress.org/?v=3.4.2</generator>
Next we have the post identifier section:
<item> <title>Your Post Title</title> <wp:comment_status>open</wp:comment_status> <wp:post_name>your-post-title</wp:post_name> <wp:post_type>post</wp:post_type>
Then, we put the comments:
<wp:comment> <wp:comment_id>351</wp:comment_id> <wp:comment_author><![CDATA[commentorname]]></wp:comment_author> <wp:comment_author_email>commentor@email.com</wp:comment_author_email> <wp:comment_author_url></wp:comment_author_url> <wp:comment_author_IP>112.27.118.101</wp:comment_author_IP> <wp:comment_date>2012-10-21 19:34:25</wp:comment_date> <wp:comment_date_gmt>2012-10-21 16:34:25</wp:comment_date_gmt> <wp:comment_content><![CDATA[replacewithcommentcontenttext]]></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>
You may probably want to write a script to loop through your comments database table/text file/etc and generate the above code in a loop for each one.
Finally, after all comments are output, we close the file with the following:
</item> </channel> </rss>
Observations:
If the post author line is missing, you will get a message saying that import will be assigned to current logged user:
“Failed to import author . Their posts will be attributed to the current user.”
Don’t forget to press Submit! after any messages you receive.
If the commend id line is missing, what you will get inserted is only the last comment!
Put any ids in the lines, they will be replaced with proper ids at import time.
<wp:comment_id>351</wp:comment_id>
And now the PHP script that does it all, for getting the comments from a mysql database table, model the result to match WordPress format and output to screen:
<?php //db configuration define("MYSQLUSER", "yourmysqluser"); // mysql username define("MYSQLPASS", "yourmysqlpass"); // mysql password define("MYSQLDB", "yourdbname"); // mysql database name //db connection if (!$db) { echo "Couldn't open database!\n"; exit; } $db = mysql_connect("localhost", MYSQLUSER, MYSQLPASS); $result = mysql_select_db(MYSQLDB, $db); if (!$result) echo('Error: Could not select database.'); $xml = <<<HEADER <?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0" xmlns:excerpt="http://wordpress.org/export/1.2/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.2/" > <channel> <language>en-US</language> <wp:wxr_version>1.2</wp:wxr_version> <generator>http://wordpress.org/?v=3.4.2</generator> <wp:author><wp:author_id>1</wp:author_id><wp:author_login>yourwordpressuser</wp:author_login><wp:author_email>youremail@mail.com</wp:author_email><wp:author_display_name><![CDATA[yourwpdisplayname]]></wp:author_display_name><wp:author_first_name><![CDATA[]]></wp:author_first_name><wp:author_last_name><![CDATA[]]></wp:author_last_name></wp:author> <item> HEADER; //blog post identifiers $title="Your post name to which you want to add comments"; $status="Approved"; $xml .= "\n<title>" . $title . "</title>\n"; $xml .= "<wp:comment_status>open</wp:comment_status>\n"; $xml .= "<wp:post_type>post</wp:post_type>\n"; //get comments from old database source $sql="select * from yourtable where your_old_article_id_column = your_old_article_id"; $res=mysql_query($sql) or die("Error: Query failed: ".mysql_error()); if (mysql_num_rows($res)!=0) { $id = 0; while($j = mysql_fetch_row($res)) { $id++; //adapt to script $author = $j[3]; $email = "an_email_address@an_email_domain.com"; $IP = $j[6]; $date = $j[2]; $comment = $j[5]; //write comment in xml $xml .= "<wp:comment>\n"; $xml .= "<wp:comment_id>".$id."</wp:comment_id>\n"; $xml .= "<wp:comment_author><![CDATA[" . $author . "]]></wp:comment_author>\n"; $xml .= "<wp:comment_author_email>" . $email . "</wp:comment_author_email>\n"; $xml .= "<wp:comment_author_url></wp:comment_author_url>\n"; $xml .= "<wp:comment_author_IP>" . $IP . "</wp:comment_author_IP>\n"; $xml .= "<wp:comment_date>" . $date . "</wp:comment_date>\n"; $xml .= "<wp:comment_date_gmt>" . $date . "</wp:comment_date_gmt>\n"; $xml .= "<wp:comment_content><![CDATA[" . $comment . "]]></wp:comment_content>\n"; if ($status == "Approved") $xml .= "<wp:comment_approved>1</wp:comment_approved>\n"; else $xml .= "<wp:comment_approved>0</wp:comment_approved>\n"; $xml .= "<wp:comment_type></wp:comment_type>\n"; $xml .= "<wp:comment_parent>0</wp:comment_parent>\n"; $xml .= "<wp:comment_user_id>0</wp:comment_user_id>\n"; $xml .= "</wp:comment>\n"; } } $xml .= "</item>\n"; $xml .= "</channel>\n"; $xml .= "</rss>\n"; echo $xml; ?>