Import Blog Posts into Blog Manager ( Open Cart )

Blog Manager by Echothemes doesnt have an official importer for blog posts from other platforms.

I wrote this code for my own use but I am sure it can be applied to most external blog systems as it works with the rss feed of a blog and makes use of the the functionality echothemes provides and vQmod.

I will try to explain everything as clearly as possible as far as I go, the code will be explained in the snippets below, just read the comments I placed in it, If you have questions just leave a comment I will reply when I have time, Keep in mind this doesnt pull in the authors, categories or the tags and it doesnt import photos, it ONLY pulls in the Date, Title, and Content of the post. It can be done but I dont have the need for it now. If you would like me to take this further drop me an email at janes{at}digibit.co.za.

Right, here we go.

Download the Package file form here. It includes everything you need ( 4 Files ).

Place the following code in a .xml file and place the file in the xml folder in the vQmod folder.

This file adds the import function to blog manager. It should be safe to update the plugin without and issues.

<modification>

	<id>Blog Manager Import</id>
	<version>1.0</version>
	<vqmver>2.4.1</vqmver>
	<author>Janes Oosthuizen - http://www.janes.co.za</author>

        <file name="admin/view/template/blog/blog.tpl">
            <operation>
                <search position="after" offset="2"><![CDATA[<a href="<?php echo $this->url->link('blog/setting', 'token=' . $this->session->data['token'], 'SSL'); ?>">]]></search>
                <add><![CDATA[
                    <a href="<?php echo $this->url->link('blog/article/importwordpress', 'token=' . $this->session->data['token'], 'SSL'); ?>">
                        <div class="iconHome"><img alt="" src="view/image/blog/icon_import.gif" /><br/><?php echo $text_import;?></div>
                    </a>
                ]]></add>
            </operation>
        </file>

        <file name="admin/language/english/blog/blog.php">
            <operation>
                <search position="after" offset="0"><![CDATA[$_['text_setting']         = 'Settings';]]></search>
                <add><![CDATA[
                    $_['text_import']         = 'Import';
                ]]></add>
            </operation>
        </file>

        <file name="admin/controller/blog/article.php">
            <operation>
                <search position="before" offset="1"><![CDATA[private function validateForm() {]]></search>
                <add><![CDATA[ 
                            public function importWordpress() {
                                // Load Language File
                                $this->data = $this->load->language('blog/import');

                                // Load Page Title
                                $this->document->setTitle($this->language->get('heading_title'));

                                // Add Blog Manager Styling
                                $this->document->addStyle('view/stylesheet/blog.css');

                                //Load Models
                                $this->load->model('blog/article');
                                $this->load->model('blog/author');

                                // Set Breadcrumbs
                                $this->data['breadcrumbs'] = array();
                                $this->data['breadcrumbs'][] = array(
                                        'text'		=> $this->language->get('text_home'),
                                        'href'		=> $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
                                        'separator' => false
                                );
                                $this->data['breadcrumbs'][] = array(
                                        'text'		=> $this->language->get('heading_title'),
                                        'href'		=> $this->url->link('blog/blog', 'token=' . $this->session->data['token'], 'SSL'),
                                        'separator' => ' <span class="separator">&#187;</span> '
                                );
                                $this->data['breadcrumbs'][] = array(
                                        'text'		=> $this->language->get('head_article'),
                                        'href'		=> $this->url->link('blog/article', 'token=' . $this->session->data['token'], 'SSL'),
                                        'separator' => ' <span class="separator">&#187;</span> '
                                );

                                // If Form is submitted Start Import
                                if($_POST){

                                     // Get Feed URl From Form
                                     $xml = simplexml_load_file($_POST['feed']);

                                     // Empty Variable to hold the results
                                     $datai = '';

                                     //Variable to count total posts imported
                                     $count = 0;

                                     // Loop through the Children of the feed
                                     foreach($xml->children() as $child) {

                                          // Loop through posts in the XML
                                          foreach($child->item as $ci){

                                              // Set Namespace to get the Post Content
                                              $postcontent = $ci->children('http://purl.org/rss/1.0/modules/content/');

                                              // Prepare array for passing through to addArticle Function
                                              $datar = array();
                                              $datar['article_description'][1]['title'] = $ci->title;

                                              // Get the Content from the namespace tag
                                              $datar['article_description'][1]['description'] = $postcontent->encoded;

                                              $datar['status'] = $_POST['enable'];
                                              $datar['comment'] = $_POST['comment'];
                                              $datar['author_id'] = $_POST['author'];

                                              // Change date format to the correct one
                                              $datar['created'] = strftime("%Y-%m-%d %H:%M:%S", strtotime($ci->pubDate));
                                              $datar['modified'] = strftime("%Y-%m-%d %H:%M:%S", strtotime($ci->pubDate));

                                              // Set Default Store
                                              $datar['article_store'][] = 0;

                                              // Add Article into Database
                                              $this->model_blog_article->addArticle($datar);

                                              // Increment Count
                                              $count++;
                                          }
                                      }

                                      // Set Results Text
                                      $this->data['datai'] = "Total Posts Imported: ".$count;
                                }

                                // Get Text for Back Button
                                $this->data['menu_setting_href'] = $this->url->link('blog/setting', 'token=' . $this->session->data['token'], 'SSL');

                                // Get all the authors for the form
                                $this->data['authors'] = $this->model_blog_author->getAuthors();

                                // Set the Template
                                 $this->template = 'blog/import.tpl';
                                 $this->children = array(
                                         'common/header',
                                         'common/footer',
                                 );

                                 // Output the HTML
                                 $this->response->setOutput($this->render());
                             }
                        ]]>
                </add>
            </operation>
        </file>

</modification>

Next you can add the Template file (import.tpl) to the blog admin template folder ( admin/view/template/blog/import.tpl ).

This is just the basic template file containing the form and when submitted the results.

This Code contains the following:

<?php echo $header; ?>
<div id="content">
   <div class="breadcrumb">
      <?php foreach ($breadcrumbs as $breadcrumb) { ?>
         <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
      <?php } ?>
   </div>

   <div class="container">
       <div id="et-header">
         <span class="em-header"><img src="view/image/blog/blog_manager.png" alt="<?php echo $heading_title; ?>" title="<?php echo $heading_title; ?>" /></span>
         <ul>
            <li><a href="<?php echo $menu_setting_href; ?>"><?php echo $text_menu_setting_back; ?></a></li>
         </ul>
       </div>
       <div id="et-content">
          <form action="" method="post">
              <table class="form blogAbout">
                <tr>
                   <td><label><?php echo $feed_url; ?></label></td>
                   <td>: <input type="text" name="feed"/> <span style="color:#ccc;"><?php echo $feed_url_c; ?></span></td>
                </tr>
                <tr>
                   <td><label><?php echo $author; ?></label></td>
                   <td>: <select name="author">
                      <?php 
                        foreach($authors as $a){
                            echo '<option value="'.$a['author_id'].'">'.$a['name'].'</option>';
                        }
                      ?>
                      </select></td>
                </tr>
                <tr>
                   <td><label><?php echo $enabled; ?></label></td>
                   <td>: <input type="text" name="enable"/></td>
                </tr>
                <tr>
                   <td><label><?php echo $comments; ?></label></td>
                   <td>: <input type="text" name="comment"/></td>
                </tr>
                <tr>
                   <td></td>
                   <td align="right"><input type="submit" class="button" value="<?php echo $submit_text; ?>"/></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <?php echo $datai; ?>
                    </td>
                </tr>
          </form>
       </div>
  </div>
</div>

<script type="text/javascript" src="view/javascript/blog/blog.js"></script>

<?php echo $footer; ?>

Then lastly:

Place the language file ( import.php ) in the admin language folder for the blog manager (admin/language/english/blog/import.php ).

This contains the text labels for the various elements.

Here is the code:

<?php
//== Heading
$_['heading_title']           = 'Blog Manager';
$_['head_import']              = 'Wordpress Importer';
$_['head_article']              = 'Wordpress Importer';

//== Menu
$_['text_menu_setting_back']       = 'Back to Settings';

$_['feed_url']      = 'Feed Url';
$_['feed_url_c']      = 'This is the url to your blog feed ex. http://www.myblog.com/feed/rss2/';
$_['author']      = 'Author';
$_['enabled']      = 'Enabled';
$_['comments']      = 'Comments';
$_['submit_text']      = 'Import Posts';

?>

That is Basically all there it to it. You should be able to drop the files in and start importing right away.

Update: The Status and Comments fields should have a value of 1 or 0. Forgot to put in dropdowns for them. 🙂

Just a note: I have developed this around the default feed that wordpress provides ( http://www.yourblog.com/feed/rss2 ). I have not tried this on other feeds. With a little changing this can be used on any feed I am sure. 

Also Before importing from wordpress, Make sure you have set the rss feed to show 9999 posts to get all posts in the feed and also make sure you have it set so the rss shows the whole post and not just a summary.

This was written in a hurry and like I said there is room for improvement.

Janes Oosthuizen

Author Janes Oosthuizen

Programmer and Tech Junky from Bloemfontein, South-Africa. I have been programming for 8 years in various languages including ( CSS, HTML, jQuery, PHP, MySQL, Wordpress and many other ).

More posts by Janes Oosthuizen

Leave a Reply