Search This Blog

Friday, 30 November 2012

Migrating Hubspot blog posts into Wordpress

Install Blog Exporter APP in Hubspot and export the posts in RSS feed format. Name it as hubspotfeed.xml

Then using following script convert the Hubspot RSS feed to Wordpress compatible RSS feed. The new generated RSS file will be created as wp-supported-feed.xml in the same directory where your PHP script file resides.


<?php
    $doc = new DOMDocument();
    $doc->load( 'hubspotfeed.xml' );
   
    $blog = $doc->getElementsByTagName( "item" );

    $xml = new XmlWriter();
    $xml->openMemory();
    $xml->startDocument('1.0', 'UTF-8');
   
        $xml->startElement('rss');

            $xml->writeAttribute('version', '2.0');
            $xml->writeAttribute('xmlns:excerpt', "http://wordpress.org/export/1.2/excerpt/");
            $xml->writeAttribute('xmlns:content', "http://purl.org/rss/1.0/modules/content/");
            $xml->writeAttribute('xmlns:wfw', "http://wellformedweb.org/CommentAPI/");
            $xml->writeAttribute('xmlns:dc', "http://purl.org/dc/elements/1.1/");
            $xml->writeAttribute('xmlns:wp', "http://wordpress.org/export/1.2/");

    try {
        $xml->startElement('channel');
            $xml->writeElement('wp:wxr_version', '1.2');

            foreach( $blog as $post ) {
           
                $xml->startElement('item');

                    $headers = $post->getElementsByTagName( "title" );
                    $title = $headers->item(0)->nodeValue;
                    $xml->writeElement('title', $title);
                   
                    //$xml->writeElement('description', "");

                    $dates= $post->getElementsByTagName( "description" );
                    $description= $dates->item(0)->nodeValue;
                    //$xml->writeElement('content:encoded', $description);
                   
                    $find = array("<br /><br />", "<br/><br/>", "&#146;");
                    $replace   = array("", "<br/><br/>", "'" );
                    $description = str_replace($find, $replace, $description);
                   
                    //$description = htmlspecialchars_decode($description);
                   
                    $xml->startElement('content:encoded');
                        $xml->writeCData($description);
                    $xml->endElement();
                   
                    $notes = $post->getElementsByTagName( "pubDate" );
                    $pubDate = $notes->item(0)->nodeValue;
                    $xml->writeElement('pubDate', $pubDate);
                    $xml->writeElement('wp:post_date', $pubDate);
                    $xml->writeElement('wp:post_date_gmt', $pubDate);
                   
                    $links = $post->getElementsByTagName( "guid" );
                    $guid = $links->item(0)->nodeValue;
                    $xml->writeElement('guid', $guid);
                   

                    $xml->writeElement('wp:comment_status', 'open');
                    $xml->writeElement('wp:ping_status', 'open');
                    $xml->writeElement('wp:status', 'publish');
                    $xml->writeElement('wp:post_type', 'post');
                    $xml->writeElement('dc:creator', 'admin');
               
                $xml->endElement();
                //break;
            }
        $xml->endElement();
    } catch(Exception $e) {
        $xml->writeElement('error', $e->getMessage());
    }

    $xml->endElement();
    $content = $xml->outputMemory(true);
    file_put_contents("wp-supported-feed.xml", $content);
?>

Login to Wordpress admin section and Navigate to "Tools => Import"
Then click on Wordpress link
Upload the wp-supported-feed.xml file and submit
Next screen select Import author: as admin in the dropdown and check the box Import Attachments and click on "Submit"
You are done all your blog posts will be imported .............

2 comments:

  1. Will this migrate the comments on the posts as well? Any suggestions for getting the comments to migrate with the posts?

    ReplyDelete
  2. And what about images, will they migrate?

    ReplyDelete