A Guide to Magento and WordPress Integration

March 30, 2015 / WodPress
Magento-WordPress-Integration

If you are an e-store owner, you might be finding it difficult to publish audience-attractive content. No wonder product definitions/descriptions are a must but it isn’t essential if that sort of content will attract any loyal visitor.

With Google becoming more sophisticated and social media becoming a dominant source of referrals. It is essential for the retailers of e-stores to gear up with content publishing if they want to retain existing buyers as well as drive in new customers.

Magento can be utilized as a blogging platform with some available add-ons but if one integrates a platform specifically for content publication. Then there are several advantages of it. Turning WordPress into an e-commerce store is surely possible but with Magento. The retailer gets flexibility and functionality which is quite tough to be acquired on a platform designed for content management.

The right way is to integrate the WordPress blog in your Magento store instead of installing WordPress separately, as the data you want requires synchronization. But both ways will be explained here.

Let’s first take a look at integrating a WordPress blog to a Magento store –

  • Manual integration by inserting codes –

Insert the following code in your Magento index.php file –

define(‘WP_USE_THEMES’, false);

require_once(‘/path/to/wordpress/directory/wp-load.php’);

By inserting this code the user will be able to utilize any WordPress functions in your Magento store. For instance – If you want to show a WordPress navigation menu in the header of your Magento store. You can add the code you are currently using for creating the nav menu in your WordPress site directly into page/template/html/header.phtml file that loads your Magento header.

  • By using an extension –

Adding a WordPress blog to an existing Magento store with Magento WordPress Integration from the FishPig extension is one of the best ways.

This extension allows the e-store owner can maintain a consistent brand identity by using the theme of the Magento store for the WordPress blog without modifying any files on either Magento or WordPress installation. One can log in to the WordPress blog through the Magento dashboard.

Since there are numerous extensions available for WordPress, it is chosen as an integration instead of the native Magento blogging solution. The extension also supports popular WordPress plugins like Yoast’s SEO plugin and Disqus comments.

Now let’s learn about Integration Magento with a WordPress site –

  • Manual integration by inserting codes –

Basic Setup –

This integration requires more coding in comparison to the previous integration. If you want to use your Magento header menu in your WordPress site. The first step required would be using this code in your WordPress header.php file as below –

<?phprequire_once”shop/app/Mage.php”;unmask(0);Mage::app(‘default’);?>

With this coding, you get the Magento Mage.php file with all the core functionality for Magento. Now you can access Magento template blocks and can load them into your header.php file.

Loading Blocks –

Magento header block can be loaded by using the following code –

<?php// Get Magento Layout$layout=Mage::app()->getLayout();

// Update Default Blocks$layout->getUpdate()->addHandle(‘default’)->load();

// Generate the layout XML and blocks$layout->generateXml()->generateBlocks();

// Access the header block and convert to HTML$header=$layout->getBlock(‘header’)->toHtml();

// Echo header block to the pageecho$header;?>

You get the default Magento layout and updated blocks that are set up under <default> XML tag. If you simply try to load the header block without inserting this code then certain blocks won’t load properly which are included in the default layout for the header or site.

By completing this step the user gets access to the header block by generating XML and blocks. After this, the block can be echoed on the WordPress page.

Sharing Session Information –

To get access to Magento session information, you need to grab the Mage session singleton by using the following code –

Mage::getSingleton(‘core/session’,array(‘name’=>’frontend’));

When you initiate the Magento store into your WordPress page simply type this code after Mage::app(‘default’). Ensure that you insert the code at the right place as there are frontend (store user information such as shopping cart) as well as admin (back-end admin panel) sessions. 

In the frontend session, you will see the value which is your encrypted session ID that enables you to access things like shopping carts. Make sure that the path is set to ‘/’.

Resolving Session Headers Already Sent Issue –

After all this code, it might happen that you get an error message. When loading the WordPress site it informs that the headers for the site have already been set.

Generally, you require to overwrite the default behavior for app/code/core/Mage/Core/Model/App.php – it tries to send the HTTP header for ‘Content-Type’. But it is already sent by WordPress and therefore, an error occurs as headers can’t be sent twice.

To resolve this issue, you need to send the ‘Content-Type’ header so that one can get access to the Magento session cookie on the WordPress site without any hassle.

  • The Plugin Method –

You can use the Magento WordPress Integration plugin for integrating WordPress blog into your Magento site.

This plugin enables users of WordPress as well as Magento to import Magento blocks into a WordPress theme. And offers shortcodes in order to add Magento products to posts and images in WordPress.

If you have an existing e-store based on WordPress and would like to integrate Magento products in a stress-free way then this plugin is a great method.

Though WordPress and Magento lack some features when used separately, when combined together they create an efficient, powerful, and fluid website. So, you don’t miss you try it.