How to use WordPress home page in Magento home page (CMS page)

Simple : ) -Ok, lets get down to business. This one is really, really simple, but powerful. In all it greatness Magento lacks the good content management solution (CMS features). Lot of people are heading down the road of integrating the WordPress and Magento. Probably everyone in web development has heard about WordPress. Personaly I like them, from Joomla, Drupal, Wordpress… But there is that little something that makes WordPress far more loving in the eyes of the client. Not to start a flame here, lets get back to the topic. Due to the lack of the proper content management in Magento those who use WordPress can easily manage the (home page in this case) content trough WordPress and make it accessible in Magento CMS page (that you can set to home page in Magento).
When would you like to do so?
Imagine you have Magento installed in root of your site and WordPress under some folder like /articles. By default WordPress is accessed via http://mysite.domain/articles and Magento is accessed trough http://mysite.domain. Our goal is to have WordPress Page (that we will title “home“) to be accessible on http://mysite.domain.
Here are the necessary steps involved:
- Create a WordPress page named “home” (under url like http://mysite.domain/articles/wp-content)
- Create a file named cmswp_home.phtml under app/design/frontend/default/yourtheme_or_default_theme/template/inchoo/ folder
- Create a Magento CMS page (if it already does not exist) name it anyway you want and then add {{block type=”core/template” template=”inchoo/cmswp_home.phtml”}}
to its content - Set the newly created Magento CMS page as home page for Magento under Magento Admin System > Configuration > Web > Default Pages > CMS Home Page
Below is the code you need to copy paste into the cmswp_home.phtml file mentioned in above steps.
< ?php
/**
* Pulls the content of post named 'home' from WordPress 'wp_posts' table
* @author Branko Ajzele
* @license GPL
*/
//Fetch database connection
$_conn = Mage::getSingleton('core/resource')->getConnection('core_write');
//Set query that retrieves the home page content
//THIS GOES IF YOU HAVE WORDPRESS AND MAGENTO IN SAME DATABSE
$_findHomePage = "SELECT post_content FROM wp_posts WHERE post_name = 'home' AND post_status = 'publish' AND post_type = 'page'";
//Set default value of home page to null, then do a safe check just to be more user friendly
$_homePageContent = null;
try {
/*
//IF YOU HAVE WORDPRESS AND MAGENTO INSTALLED EACH IN ITS OWN DATABASE; THEN USE
$conf = array(
'host' => 'localhost',
'username' => 'wordpress_db_username',
'password' => 'wordpress_db_password',
'dbname' => 'wordpress_db_name'
);
$_resource = Mage::getSingleton('core/resource');
//Create new connection to new server and new databse
$_conn = $_resource->createConnection('place_some_free_random_resource_name_here', 'pdo_mysql', $conf);
*/
$_homePageContent = $_conn->fetchOne($_findHomePage);
}
catch (Exception $ex) {
//SEND EXCEPTION INFO TO ADMIN?
/*
* USE $emailSmtpConf, $transport and $mail->send($transport); in case you need to specify SMTP manualy
$emailSmtpConf = array(
'auth' => 'login',
'ssl' => 'tls',
'username' => 'some_email@gmail.com',
'password' => 'some_pass_here'
);
$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $emailSmtpConf);
*/
$_sendTo_email = 'ajzele@somemail.com';
$_sendFrom_email = 'branko.ajzele@somemail.com';
//Notify each item author about purchase
$mail = new Zend_Mail();
$mail->addTo($_sendTo_email, 'Branko Ajzele');
$mail->setFrom($_sendFrom_email, 'Branko Ajzele');
$mail->setSubject('Invalid HOME PAGE on site detected');
$mail->setBodyHtml('
<h1>Invalid HOME PAGE on site detected</h1>
<p>There seem to be some issues with home page on your site. Please take a look at it.</p>
<p>Could be that WordPress page has been disabled and this made the query for page return null.</p>
');
try {
//Try to send email
//$mail->send($transport);
$mail->send();
}
catch (Exception $ex) {
//echo $ex->getMessage();
}
}
?>
< ?php if($_homePageContent): ?>
< ?php echo $_homePageContent ?>
< ?php else: ?>
<h1>Home page unavailable</h1>
<p>Seems like home page is unavailable at the moment. We apologize for the inconvenience.</p>
< ?php endif; ?>
That’s it.
If you wish to have more control over what is outputted take a look $_findHomePage variable and the query behind it.
Hope you find this useful.
Cheers.
By Branko Ajzele from Inchoo.net
Related posts:

Leave your response!