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

Featured Image

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.

Interested in hiring us?

Have a chat with us. You would be surprised how small changes can make your business even more successful.


20 comments

  1. Hi All ,

    I have wordpress and Magento installed in two different servers . I need to use wp header and wp footer in Magento Fish pig module.

    Can somebody help me in that ?

  2. The fishpig extension has this feature built in now so you can get the wordpress blog listing or page as the magento homepage easily

  3. What do I use in place of
    ‘place_some_free_random_resource_name_here’
    on line 32?

    My magento and wordpress are on the same server but different databases. Do I comment out line 14? Should I uncomment lines 22-32?

  4. I have found that this implementation has one enormous problem. Auto generated content does not show header or footer. Hopefully this saves someone a lot of time.

  5. Could someone possibly expand on:
    1) How to add the entire pages content less header and footer rather than just pulling the posts
    2) What I change within the file to have it pull a different wp page? I am not using it for the home page, but am for the about us, faqs, etc.

    Sorry for the lack of knowledge. I actually got it to do exactly what it is designed to though. :)

  6. Hi Branko

    This is really cool :-) Thanks so much for sharing.

    I’ve got two questions if I may :

    1) Do you know if the content that is pulled back is CACHED in any way?

    2) I’m wanting to use this to pull back ANY page from WordPress – so pass in a parameter somehow (perhaps the page name or some such) – do you know if this would be easy to do?

    Thanks in advance :-)

    Mark

  7. Can something like this be used with Xsitepro? What would be required to get this integration. Creating a template in Xsitepro and overlaying Magneto??? Could this work?

  8. i’m using lazzymonks wordpress extension on my magento shop.
    i’d love to open wordpress page instead cms pages. i’ve tried this howto but it’s not workin’…
    nothing happens.

  9. busy with day job and paying Magento gigs so no documentation yet but see http://westgardiner.net
    prototype pulls in wordpress content and it’s styled with Magento css.
    all pages with the footer
    “westgardiner.netContent is proudly powered by WordPress”
    are wordpress posts. Just add Postie WordPress mod and you can email content directly to Magento.
    of course it’s a work in progress…

    fyi inchoo, activecodeline and snippi are the best for Magento info hands down…

    Ray

  10. I have had the same problem. I never thought of doing the home page this way. I have some other ideas, but they do require manual coding. Using wordpress would be perfect for that.

    There was a wordpress integration module for magento, but its not working for me.

    I have thought of reversing it and building a plugin to integrate Magento directly into WordPress.

  11. @LekeFly, @Rosco

    I am not sure what you mean by demo here? Not much you would see. Imagine a WordPress page with custom content. Now imagine you take that content (which is actually a HTML string) and output it inside the Magento block.

    Its like imagining the content of these commecnt being pulled inside one of the Magento blocks.

    I was merely trying to show a concept, a possible way of using things.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.