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

Multi store / website setup on different domains

Thursday 29 November 2012

Wordpress multisite

Functions can be found at http://codex.wordpress.org/WPMU_Functions

Code to get any blog pages by blog id as below:

switch_to_blog( $blog_id );
$pages = get_pages($args);
foreach ( $pages as $page ) {
        echo $page->post_title;
}
restore_current_blog();

Code to get all the blogs in current site:

global $wpdb;
$blogs_info = array();
$query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' AND spam = '0' AND deleted = '0' AND archived = '0' AND mature = '0' AND public = '1'";
$site_blogs = $wpdb->get_results($wpdb->prepare($query)); // get all subsite blog ids
if(count($site_blogs) > 0) {
    foreach( $site_blogs AS $site_blog ) {
          $details = get_blog_details($site_blog->blog_id);
     }
}

Tuesday 20 November 2012

Working with date functions in Magento


The date time will be calculated based on the lacale timezone settings in administration:

    $anyDate = '2012-11-21 13:00:00';
    $dateTimestamp = Mage::getModel('core/date')->timestamp(strtotime($anyDate));
    echo $currentDate = date('Y-m-d H:i:s', $dateTimestamp);

Click here for more details




Other PHP scripts to find the offset for the timezone

/** Script to calculate the offset seconds fro the timezone */
$timezone = new DateTimeZone("Europe/London");
$offset = $timezone->getOffset(new DateTime("now")); // Offset in seconds
echo ($offset < 0 ? '-' : '+').round($offset/3600).'00'; // prints "+1100"

/** Script to convert the date and time to specific timezone */
$value = "2012-09-21 13:00:00";
$date = new DateTime($value);
$date->setTimezone(new DateTimeZone('Europe/London'));
echo $date->format('Y-m-d H:i:s');

Monday 19 November 2012

Magento: Get current Module Name, Controller Name and Action Name

$req = Mage::app()->getRequest();
$req->getModuleName();
$req->getControllerName();
$req->getActionName();

Thursday 8 November 2012

Magento: Create new payment method

Magento: Display Currency Switcher in Header

Add the following XML in layout/directory.xml between <default> and </default>

        <reference name="header">
            <block type="directory/currency" name="top.currency"  template="directory/currency.phtml"/>
        </reference>

Add following code in header.phtml

<?php echo $this->getChildHtml('top.currency') ?>

Magento: Change the store currency

$store = Mage::app()->getStore();

$store->setCurrentCurrencyCode('GBP');

Note: 
Make sure the currency is enabled for the specific store
Make sure the currency conversion rate has been populated and saved

Tuesday 6 November 2012

Magento: Events and Observers

One of the greatest feature in Magento is Events and Observers.

You can add any functionality for the specific Event without following:
1. Modifying core files
2. Copy core file to local folder
3. Override Core files
4. Extend core methods

Here are the links for best examples:
  1. http://codemagento.com/2011/04/observers-and-dispatching-events/
  2. http://magedev.com/2009/08/11/magento-events-create-update/
  3. http://magedev.com/2010/10/15/adding-event-observer-on-the-fly/
You can create custom events in your Own module as well

Using events is the best way of extending Magento functionality......