• WordPress XML Import Format – Comments
    by ShibaShake on

    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 -

    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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    $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_date_gmt>" . get_gmt_from_date($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";

    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

    $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.

    Related Articles

    10 Comments
    1. Interesting material – I enjoyed it very much! Rosie Mikulec

      9:08 pm on September 26th, 2011 Reply
    2. Magnus

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

      8:20 pm on March 25th, 2011 Reply
      • 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.

        3:37 pm on March 28th, 2011 Reply
    3. [...] Puis,du côté du template, on effectue toutes les transformations nécessaires afin de satisfaire au format d’export WordPress. Pour en savoir plsu sur ce format, fouillez tout simplement le script d’export de WordPress, et parcourez cette ressource. [...]

      4:06 am on December 14th, 2010 Reply
    4. [...] WordPress XML Import Format – Comments (tags: wordpress import export xml format wp comments) AKPC_IDS += "483,";Popularity: unranked [?] [...]

      7:02 am on October 17th, 2010 Reply
    5. [...] from: WordPress XML Import Format – Comments Tags: export, format, [...]

      11:34 am on October 16th, 2010 Reply
    6. 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 :)

      4:32 pm on July 18th, 2009 Reply
    7. Now I am quite confused! Thanks for the info; I will try to digest it after I have had some sleep…

      4:31 pm on July 18th, 2009 Reply
    8. This is definitely valuable information. Thank you.

      4:30 pm on July 18th, 2009 Reply
    9. Hey thanks, Shiba! I was trying to figure out this very thing =D

      4:29 pm on July 18th, 2009 Reply

    Leave a Reply

    Your email address will not be published.

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

    search button search button
    rss