WordPress Comments - XML Import Format
74
In this article we consider how to write out comments data in the WordPress XML import format.
Once you do this, you can easily import the generated XML file into WordPress by using the Import functionality within your WordPress dashboard.
We assume that you already have the comment data extracted in a PHP script elsewhere.
The first thing that goes into a WordPress XML comments file is the XML file header.
WordPress Comments XML Header
$xml = <<<HEADER <?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 Manage: Import in the blog's admin panels. --> <!-- 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/MU" created="2008-11-17 22:40"--> <rss version="2.0" 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/" > <channel> <item> HEADER;
Note - because there are multiple lines of text, we simplify the writing of the header by using the heredoc PHP string syntax.
After writing the header, you want to write in the TITLE of the WordPress page used to store your comments. If the title used does not currently exist within your WordPress blog, a new page will be automatically created when you import the XML file into WordPress.
WordPress Page Title
$xml .= "\n<title>" . $title . "</title>\n"; $xml .= "<wp:comment_status>open</wp:comment_status>\n"; $xml .= "<wp:post_type>page</wp:post_type>\n"; $xml .= "<wp:status>private</wp:status>\n";
Here, we are saving the comments into a WordPress page, and setting that page to be private. You can make the WordPress page public later on if you so desire.
Now, we are ready to write out each of our WordPress comments. The same syntax must be used in writing out each comment. We assume that -
- $status contains the status of the comment (Approved or something else).
- $author contains the name of the comment author.
- $link contains the website of the comment author.
- $IP contains the ip address of the comment author.
- $date contains the date when the comment was first posted.
- $comment contains the text of the comment.
WordPress Comment Format
$xml .= "<wp:comment>\n";
$xml .= "<wp:comment_author><![CDATA[" . $author . "]]></wp:comment_author>\n";
$xml .= "<wp:comment_author_email>" . $author . "@hubpages.com</wp:comment_author_email>\n";
if ($link == null)
$xml .= "<wp:comment_author_url></wp:comment_author_url>\n";
else
$xml .= "<wp:comment_author_url>http://www.hubpages.com" . $link . "</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_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";The CDATA tags are used in lines 3 and 14 so that the contents will only be interpreted as text and not as XML markup. This is important because certain user names and comment content may contain HTML markup tags such as paragraph tags (<p>,</p>). You want to transfer these tags as is into WordPress so that the comments that show up in WordPress will retain those formatting options.
If you do not use the CDATA tags, WordPress may try to interpret those tags as part of the XML file it is parsing and run into errors during the import process.
After writing all your comments, close the WordPress XML file with the following footer.
WordPress XML Footer
$xml .= "</item>\n"; $xml .= "</channel>\n"; $xml .= "</rss>\n"; echo $xml;
Echoing the results will make it appear on your web browser. Alternatively, you can return the results to the calling process.
If you are sending the results to your web browser, you may save it by using the Save As or Save Page As option in your web browser.
ShibaShake Blog
- Your Wedding
Your wedding is a very special and wondrous event that you will likely only experience once in your life. Therefore, it is only right that you make it everything that you always imagined it to be. If... - 3 months ago
- The Women of HubPages
One of the first things you will notice on HubPages land is that there are many talented and wonderful women authors. We will start our journey with them … The Hub Sisterhood is a collection... - 3 months ago
PrintShare it! — Rate it: up down flag this hub
This is definitely valuable information. Thank you.
Now I am quite confused! Thanks for the info; I will try to digest it after I have had some sleep…
Thank you all for dropping by :)
Hahaha Nancy, yeah definitely not a hub to read right before bed. Although, maybe it has a soporific effect :)
WordPress in the News
- WordPress To Posterous Users: Graduate to a Full BlogMashable8 hours ago
In an effort to remind you that WordPress can be just as flexible and e-mail-friendly as Posterous , the company has released a Posterous importer for users who want to “graduate” from the mini-blogging site to “a full blog with the features, flexibility and reliability of WordPress.com.” The new WordPress.com utility can be found via the Tools section within the Dashboard. All that’s required ...
- Just another WordPress weblogCameroon-Tribune26 hours ago
Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
- Wordpress for Android - A Blogger's Dream AppLinux Today4 days ago
2 live 2 blog: "Android devices will now allow users to post articles on their Wordpress blogs through the newly released Wordpress for Android app."













Am I dead, yet? says:
7 months ago
Hey thanks, Shiba! I was trying to figure out this very thing =D