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
<br /> $xml = <<<HEADER<br /> <?xml version="1.0" encoding="UTF-8"?><br /> <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. --><br /> <!-- It contains information about your blog's posts, comments, and categories. --><br /> <!-- You may use this file to transfer that content from one site to another. --><br /> <!-- This file is not intended to serve as a complete backup of your blog. --></p> <p><!-- To import this information into a WordPress blog follow these steps. --><br /> <!-- 1. Log into that blog as an administrator. --><br /> <!-- 2. Go to Manage: Import in the blog's admin panels. --><br /> <!-- 3. Choose "WordPress" from the list. --><br /> <!-- 4. Upload this file using the form provided on that page. --><br /> <!-- 5. You will first be asked to map the authors in this export file to users --><br /> <!-- on the blog. For each author, you may choose to map to an --><br /> <!-- existing user on the blog or to create a new user --><br /> <!-- 6. WordPress will then import each of the posts, comments, and categories --><br /> <!-- contained in this file into your blog --></p> <p><!-- generator="WordPress/MU" created="2008-11-17 22:40"--><br /> <rss version="2.0"<br /> xmlns:content="http://purl.org/rss/1.0/modules/content/"<br /> xmlns:wfw="http://wellformedweb.org/CommentAPI/"<br /> xmlns:dc="http://purl.org/dc/elements/1.1/"<br /> xmlns:wp="http://wordpress.org/export/1.0/"<br /> ></p> <p><channel><br /> <item><br /> HEADER;<br />
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
<br /> $xml .= "\n<title>" . $title . "</title>\n";</p> <p>$xml .= "<wp:comment_status>open</wp:comment_status>\n";<br /> $xml .= "<wp:post_type>page</wp:post_type>\n";<br /> $xml .= "<wp:status>private</wp:status>\n";<br />
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
<br /> $xml .= "<wp:comment>\n";</p> <p>$xml .= "<wp:comment_author><![CDATA[" . $author . "]]></wp:comment_author>\n";</p> <p>$xml .= "<wp:comment_author_email>" . $author . "@hubpages.com</wp:comment_author_email>\n";</p> <p>if ($link == null)<br /> $xml .= "<wp:comment_author_url></wp:comment_author_url>\n";<br /> else<br /> $xml .= "<wp:comment_author_url>http://www.hubpages.com" . $link . "</wp:comment_author_url>\n";</p> <p>$xml .= "<wp:comment_author_IP>" . $IP . "</wp:comment_author_IP>\n";<br /> $xml .= "<wp:comment_date>" . $date . "</wp:comment_date>\n";<br /> $xml .= "<wp:comment_date_gmt>" . get_gmt_from_date($date) . "</wp:comment_date_gmt>\n";<br /> $xml .= "<wp:comment_content><![CDATA[" . $comment . "]]></wp:comment_content>\n";</p> <p>if ($status == "Approved")<br /> $xml .= "<wp:comment_approved>1</wp:comment_approved>\n";<br /> else<br /> $xml .= "<wp:comment_approved>0</wp:comment_approved>\n";</p> <p>$xml .= "<wp:comment_type></wp:comment_type>\n";<br /> $xml .= "<wp:comment_parent>0</wp:comment_parent>\n";<br /> $xml .= "<wp:comment_user_id>0</wp:comment_user_id>\n";</p> <p>$xml .= "</wp:comment>\n";<br />
The CDATA tags are used in lines 3 and 15 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
<br /> $xml .= "</item>\n";<br /> $xml .= "</channel>\n";<br /> $xml .= "</rss>\n";</p> <p>echo $xml;<br />
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.
vikarm says
thanks its working perfectly and i want to get comments by post id please post the code
Rosie Mikulec says
Interesting material – I enjoyed it very much! Rosie Mikulec
Magnus says
It seems comment metadata is not exported, is that correct? Do you know any best practises of how to handle (export/import) metadata?
ShibaShake says
Sorry, I haven’t done that before. The easiest way is probably to export something simple and look at the XML source that gets generated by WordPress.
shibashake says
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 🙂
Nancy's Niche says
Now I am quite confused! Thanks for the info; I will try to digest it after I have had some sleep…
dohn121 says
This is definitely valuable information. Thank you.
Am I dead, yet? says
Hey thanks, Shiba! I was trying to figure out this very thing =D