Shiba

Adventures in WordPress

  • Home
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Programming / WordPress XML Import Format – Comments

WordPress XML Import Format – Comments

by ShibaShake 11 Comments

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 –

  1. $status contains the status of the comment (Approved or something else).
  2. $author contains the name of the comment author.
  3. $link contains the website of the comment author.
  4. $IP contains the ip address of the comment author.
  5. $date contains the date when the comment was first posted.
  6. $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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comments

  1. vikarm says

    March 11, 2015 at 4:50 am

    thanks its working perfectly and i want to get comments by post id please post the code

    Reply
  2. Rosie Mikulec says

    September 26, 2011 at 9:08 pm

    Interesting material – I enjoyed it very much! Rosie Mikulec

    Reply
  3. Magnus says

    March 25, 2011 at 8:20 pm

    It seems comment metadata is not exported, is that correct? Do you know any best practises of how to handle (export/import) metadata?

    Reply
    • ShibaShake says

      March 28, 2011 at 3:37 pm

      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.

      Reply
  4. shibashake says

    July 18, 2009 at 4:32 pm

    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 🙂

    Reply
  5. Nancy's Niche says

    July 18, 2009 at 4:31 pm

    Now I am quite confused! Thanks for the info; I will try to digest it after I have had some sleep…

    Reply
  6. dohn121 says

    July 18, 2009 at 4:30 pm

    This is definitely valuable information. Thank you.

    Reply
  7. Am I dead, yet? says

    July 18, 2009 at 4:29 pm

    Hey thanks, Shiba! I was trying to figure out this very thing =D

    Reply

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...
  • How to Add Admin Columns in WordPress (7)
    • Andy Globe
      - Hi Friends, I am facing two problem in WordPress admin1. Load custom CSS for wp-admin on user-role base (editor) 2. ...
  • Example blog front-page using excerpts and the Shiba Theme.Optimize Your WordPress Plugins with Tags (5)
    • DF
      - thanks, i went the other way and added a filter if pages I wanted.
  • WordPress Search Widget – How to Style It (57)
    • Nelson
      - Tanks master - Fine
  • Update custom inputs with the proper data using Javascript.Expand the WordPress Quick Edit Menu (59)
    • Mike G
      - This is exactly what is happening to me. It is updating the value in the database and in the column, but I have to refresh ...

Copyright © 2022 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·