Update (May 11, 2010): I created a WordPress plugin for this one. Check it out here.
You have a free WordPress blog that has been online for sometime. You love its simple yet powerful way of managing your content so you’ve decided to have it hosted to a different provider in order to have a more granular control to its code and functionality. Eventually, you needed cash to support the hosting expenses so you decided to utilize your blog for e-commerce use. That’s when Magento comes in to the scene. You want to find a way to use Magento as your backend while pulling your catalog and customer information to be posted to WordPress site. This is what my article is for.
Since your WordPress will serve as the frontend, you don’t have to worry about where the database for Magento will be located as long as you have a local access to the Mage.php file in order to extend all Magento’s functionality to your WordPress pages. My current setup is still the same with the rest of my post here having ‘htdocs’ as my root directory, magento has its own subdirectory ‘htdocs/magento’ as well as wordpress in ‘htdocs/wordpress’. The goal here is to able to use Magento as if it is a native function within our WordPress installation.
Since there is an existing function collision between Magento and WordPress because both application has an existing translator function named __(), our first task is to automatically detect if the function already exists and disable it in Magento and run as usual if it doesn’t.
To do that, locate the file below in your Magento installation:
Copy the file functions.php below from your Magento core folder
path:to-your-htdocs/magento/app/code/core/Mage/Core/functions.php
and paste it in the Magento local folder which can be found below and open it for editing (create needed folders if it doesn’t exists):
path:to-your-htdocs/magento/app/code/local/Mage/Core/functions.php
Locate the function __() or go to line 93:
function __()
{
return Mage::app()->getTranslator()->translate(func_get_args());
}
replace it with this:
if (!function_exists('__')) {
function __()
{
return Mage::app()->getTranslator()->translate(func_get_args());
}
}
Why did I choose to disable Magento’s translator function instead of WordPress’? It is because in Magento, it has already been marked as deprecated in version 1.3 and searching throughout the installation I didn’t see any file that uses that function.
Now that the function collision has been solved, let’s proceed in modifying WordPress to include our Mage.php file. Locate and open the WordPress file below:
path-to-your-root-htdocs/wordpress/wp-includes/functions.php
Scroll down to the end of the file. Add the codes below right after the last function statement which is after line 4122
/**
* Run Magento's Mage.php within WordPress
*
* @author Richard Feraro <richardferaro@gmail.com>
* @link https://mysillypointofview.wordpress.com
*
* @return object
*/
function magento($name = "frontend") {
// Include Magento application
require_once ( "../magento/app/Mage.php" );
umask(0);
// Initialize Magento
Mage::app("default");
return Mage::getSingleton("core/session", array("name" => $name));
}
That’s it! I know it’s weird that it isn’t like the solution given by others that require modifying a lot of files. Nevertheless, I tested it and it does work. You just have to make sure that whenever you use the magento() function to any file within WordPress, always place it at the top most part of the code where there’s no header request or else you will get a similar error below:
Fatal error: Uncaught exception 'Exception' with message 'Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at path:to-your-htdocs\wordpress\wp-content\themes\twentyten\index.php:19) in path:to-your-htdocs\magento\app\code\core\Mage\Core\Model\Session\Abstract\Varien.php on line 115' in path:to-your-htdocs\magento\app\code\core\Mage\Core\functions.php:245 Stack trace: #0 [internal function]: mageCoreErrorHandler(2, 'session_start()...', 'path:to-your-htdocs...', 115, Array) #1 path:to-your-htdocs\magento\app\code\core\Mage\Core\Model\Session\Abstract\Varien.php(115): session_start() #2 path:to-your-htdocs\magento\app\code\core\Mage\Core\Model\Session\Abstract\Varien.php(155): Mage_Core_Model_Session_Abstract_Varien->start('frontend') #3 path:to-your-htdocs\magento\app\code\core\Mage\Core\Model\Session\Abstract.php(84): Mage_Core_Model_Session_Abstract_Varien->init('core', 'frontend') #4 path:to-your-htdocs\magento\app\code\core\Mage\Core\Model\Ses in path:to-your-htdocs\magento\app\code\core\Mage\Core\functions.php on line 245
An example of how to use this is to check whether a customer is logged in or not. To do this, open the index.php of WordPress’ default theme which is Twenty Ten 0.7:
path-to-your-root-htdocs/wordpress/wp-content/themes/twentyten/index.php
Just below line 15, add the magento() function. It should look like the codes below:
<?php /** * The main template file * * This is the most generic template file in a WordPress theme * and one of the two required files for a theme (the other being style.css). * It is used to display a page when nothing more specific matches a query. * E.g., it puts together the home page when no home.php file exists. * Learn more: http://codex.wordpress.org/Template_Hierarchy * * @package WordPress * @subpackage Twenty Ten * @since 3.0.0 */ magento(); ?> <?php get_header(); ?>
Add the code below marked as Magento’s custom greeting right after the get_header() function of WordPress in the same file:
<?php get_header(); ?>
<!-- Magento's custom greeting -->
<div style="font-size: 15px; margin-bottom: 15px; border-bottom: 1px solid #000; padding-bottom: 10px;">
<?php
$session = Mage::getSingleton("customer/session");
$magento_message = "Welcome ";
// Generate a personalize greeting
if($session->isLoggedIn()){
$magento_message .= $session->getCustomer()->getData('firstname').' ';
$magento_message .= $session->getCustomer()->getData('lastname').'!';
}else{
$magento_message .= "Guest!";
}
echo $magento_message;
//echo "<br>";
//print_r($session);
?>
</div>
<!-- End of Magento's custom greeting -->
The purpose of the code change above is to display a ‘Welcome [customer name here]’ when a customer is logged in, and show ‘Welcome Guest’ when they are not.
Next we have to edit the file below to allow us to use the default login page of WordPress as entry point for Magento also. We assumed here that both Magento and WordPress has the same list of user credentials. Your setup maybe different as to which user’s database to use so it’s up to you how to implement it.
path-to-your-root-htdocs/wordpress/wp-includes/user.php
Locate the function wp_authenticate_username_password() and find the similar code below. I found mine at line 108.
if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) )
return new WP_Error('incorrect_password', sprintf(__('<strong>ERROR</strong>: Incorrect password. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), site_url('wp-login.php?action=lostpassword', 'login')));
Right after the code above, add the following code starting at line 111 to make it similar to the code below. This update allows us to run the login request of Magento within WordPress:
if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) )
return new WP_Error('incorrect_password', sprintf(__('<strong>ERROR</strong>: Incorrect password. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), site_url('wp-login.php?action=lostpassword', 'login')));
// Start Magento
magento();
$session = Mage::getSingleton("customer/session");
try{
$login = $session->login($username, $password);
}catch(Exception $e){
// Do nothing
}
Finally, to call the Magento’s logout function, locate the file below and open it:
path-to-your-root-htdocs/wordpress/wp-login.php
Find the switch case statement at line 352. Add the necessary code to make it similar to the code below and save it:
switch ($action) {
case 'logout' :
check_admin_referer('log-out');
// Start Magento
magento();
Mage::getSingleton("customer/session")->logout();
wp_logout();
Now test your WordPress by accessing the homepage at http://localhost/wordpress/. It should display the ‘Welcome Guest’ similar to the image below with a cookie named as ‘frontend’ in your Firebug cookie tab. That is your Magento cookie.
Clicking the login for WordPress redirects us to the login form like the one below while still showing the ‘frontend’ cookie. Since I have a customer with same credentials on both Magento (Customer) and WordPress (set as Subscriber), all I have to do is to use the username and password to login in the form below.
At this point, your Magento session should be running with an active customer session. To check if it does, go back to your homepage by clicking the top header text or going to http://localhost/wordpress/. It should display now the welcome message with your customer’s name.
I hope this article will help you get started in using Magento’s functionality within WordPress.






Apr 09, 2010 @ 09:16:03
Thanks for your effort. I like what you did. UI will be trying your solution but the only question I have is if your solution can bring not just the welcome message from magento but the header or footer too
Apr 09, 2010 @ 14:34:09
Yes, it is possible. Check the code below:
magento(); // Magento layout $magento_block = Mage::getSingleton('core/layout'); // Header $header = $magento_block->createBlock('Page/Html_Header'); $header->setTemplate('page/html/header.phtml'); echo $header->toHTML(); // Your WordPress body container here // Footer $footer = $magento_block->createBlock('Page/Html_Footer'); $footer->setTemplate('page/html/footer.phtml'); echo $footer->toHTML();Apr 11, 2010 @ 00:34:13
Hi Richard,
How would you bring the header including navigation and the works over to wordpress? My site is http://www.incego.com/m/gold, which is a slightly modified Modern theme. We have the welcome message, cart, login, etc. links, logo image and catalog navigation and cms links.
Appreciate any pointers you can provide.
Stephen
Apr 11, 2010 @ 05:37:36
Hello Stephen,
It’s in the comments area. Check out the codes I posted right after vesvello’s question.
Apr 11, 2010 @ 00:50:11
Just saw the other comments after my page refreshed. I’ll explore based on your other examples.
Thanks!
Apr 09, 2010 @ 23:59:25
That is a really interesting take on WP/Mage integration. We always use the store itself as the primary for more flexibility and control over the layout (WP is pretty basic!), but a good article nonetheless!
Apr 10, 2010 @ 11:23:45
Thanks Ben!
Apr 10, 2010 @ 10:10:25
Its perfect. My next question is:
I need to get the current store id form outside magento. (1 is English and 3 is Spanish).
In theory, the code I need is:
require_once ( “../app/Mage.php” );
umask(0);
// Initialize Magento
Mage::app(“default”);
$idioma = Mage::app()->getLocale()->getDefaultLocale();
Always return 1 (because is the default, obviusly) It doesnt matter if I have choose Spanish before I open the blog) Its hard to explain, but if you see my page http://www.talkingwebs.net you can understand. If I open home in Spanish (everything will be in Spanish)… if I go to the navigation bar and click on blog.. everything except the “content” (=blog) will be in Spanish (header, footer, sidebar, etc) but the “content will be in English because the code below I have in index.php (wp-content-theme)
require_once ( “../app/Mage.php” );
umask(0);
// Initialize Magento
Mage::app(“default”);
$idioma = Mage::app()->getLocale()->getDefaultLocale();
returns always 1
Apr 10, 2010 @ 11:45:06
If you want users to select from a list of locales, you can use the method:
and then after they selected a locale, use the method setLocale() or setDefaultLocale(), whichever is applicable to you.
You may also check the source code for more options:
http://docs.magentocommerce.com/Mage_Core/Mage_Core_Model_Locale.html
Apr 13, 2010 @ 23:29:36
Richard, awesome post. I just wanted to clarify whether in your example Magento and WordPress are installed into the same database or not.
Thanks,
Mike
Apr 13, 2010 @ 23:43:41
Thanks Michael!
In my example, WordPress and Magento have a different database for each other. Also it doesn’t matter if they are both in one database, separate database in one location or each located in a remote server as long as WordPress is able to access Mage.php
wp-popular.com » Blog Archive » How to use Magento’s session within WordPress « My Silly Point of View
Apr 14, 2010 @ 11:22:29
Apr 15, 2010 @ 17:59:02
all thing is very well.but when i go to the magento site it is already show “Default welcome msg!”
thanks
Apr 15, 2010 @ 20:25:23
Are you trying to show the header and footer of Magento to WordPress? If yes, then that’s correct. It’s your HTML block from Magento. Try viewing the source and you will see the output of your .phtml
Apr 16, 2010 @ 15:06:09
thanks for your reply.
i use wamp in windows2003
and the wordpress in the /wordpress
magento in the /magento
and i use the firecookie and found the /wordpress’s cookies path is /wordpress
/magento’s cookies path is /magento
perhaps,that is why the headeris different between wordpress and magento .
thanks
Apr 16, 2010 @ 15:53:11
The cookie path has nothing to do with it. The output has no styling because of your CSS not present in WordPress. The output is correct based on the default header.phtml found in the directory below:
path://to-your-magento/app/design/frontend/base/default/template/page/html/header.phtmlCheck the code at the directory below to see how it works:
path://to-your-magento/app/code/core/Mage/Page/Block/HtmlApr 16, 2010 @ 22:45:03
Hi Richard,
Very timely article for me. I was about to start copying source code into Magento pages. I do have one questions regarding the breadcrumbs Magento’s category navigation. Do you see your method being able to make these features work?
For example, if am viewing the products of Magento (Home>Store>Accessories) in WP and I click Home in the breadcrumb trail, is it going to take me to WP’s home or Magento’s.
Thank you for your help and your work.
Apr 17, 2010 @ 05:52:25
Hello Peaker,
Yes it can, but the default code will pull Magento’s URL. You can implement your own custom breadcrumb by either pulling the original breadcrumb from Magento and then overwrite portions of URLs (using str_replace()) into your defined pages or you can use the built-in addCrumb() method:
$breadcrumbs->addCrumb('home', array( 'label'=>'this is home', 'title'=>'view homepage', 'link'=>'http://www.your-defined-page.com' ) );For more details, check the file
path://to-your-magento/app/core/Mage/Catalog/Block/Breadcrumb.phpThanks :p
Apr 20, 2010 @ 01:30:17
Thanks for the reply Richard! I will give it a shot.
Apr 20, 2010 @ 04:47:42
What if you don’t want to assume that both Magento and WordPress has the same list of user credentials and that all you want is the global nav (Welcome, John Smith! My Account My Wishlist (1 item) My Cart Checkout Log Out) to display the session info in while viewing WP pages?
I have skinned my WP and Magento pages to look identical, but the session information therefore does not work. Meaning that when I login (to Magento) and add some products to my cart, that Welcome message reflects the change (My Cart (1 item)), but as soon as I navigate to a WP page, Welcome message does not reflect the change and still states “Welcome Guest!”. I tried following your example to pull in the header and footer from Magento. I was able to do both, but the when I tried to do a similar script to pull in the top.links.phtml, I had no success.
magento();
// Magento layout
$magento_block = Mage::getSingleton(‘core/layout’);
// TopLinks
$toplinks = $magento_block->createBlock(‘Page/Html_toplinks’);
$toplinks->setTemplate(‘page/html/top.links.phtml’);
echo $toplinks->toHTML();
// Header
$header = $magento_block->createBlock(‘Page/Html_Header’);
$header->setTemplate(‘page/html/header.phtml’);
echo $header->toHTML();
Am I going about this wrong?
Apr 20, 2010 @ 06:40:07
Hi Richard,
I figured it out. This is awesome! Thank you for your research. Using your example and the thread below, I was able to pull my toplinks session data from Magento into WP. Now customers’ session info is displayed in all my WP pages.
http://www.magentocommerce.com/boards/viewthread/17459/#t60477
Here’s my code.
isLoggedIn()){
$magento_message .= $session->getCustomer()->getData(‘firstname’).’ ‘;
$magento_message .= $session->getCustomer()->getData(‘lastname’).’!’;
}else{
$magento_message .= “Guest!”;
}
# Initiate Blocks
$linksBlock = $magento_block->createBlock(“page/template_links”);
$checkoutLinksBlock = $magento_block->createBlock(“checkout/links”);
$checkoutLinksBlock->setParentBlock($linksBlock);
$wishlistLinksBlock = $magento_block->createBlock(‘wishlist/links’);
$wishlistLinksBlock->setParentBlock($linksBlock);
# Add Links
$linksBlock->addLink($linksBlock->__(‘My Account’), ‘customer/account’, $linksBlock->__(‘My Account’), true, array(), 10, ‘class=”first”‘);
$wishlistLinksBlock->addWishlistLink();
$checkoutLinksBlock->addCartLink();
$checkoutLinksBlock->addCheckoutLink();
if ($session->isLoggedIn()) {
$linksBlock->addLink($linksBlock->__(‘Log Out’), ‘customer/account/logout’, $linksBlock->__(‘Log Out’), true, array(), 100, ‘class=”last”‘);
} else {
$linksBlock->addLink($linksBlock->__(‘Log In’), ‘customer/account/login’, $linksBlock->__(‘Log In’), true, array(), 100, ‘class=”last”‘);
}
echo $magento_message.”.$linksBlock->renderView().”;
?>
Apr 20, 2010 @ 10:49:59
That’s great, peaker!
Glad that my script helped you get started figuring out on your own how to customize your WP with Magento session running in it.
Thanks for the feedback 🙂
Apr 23, 2010 @ 23:25:05
hi Richard
great script! I have an almost solution for my needs.
just needed to know how I can render childhtml, the code below is whats in my header.phtml
<a href="getUrl(”) ?>” title=”getLogoAlt() ?>” class=”logo”>
<img src="getLogoSrc() ?>” alt=”getLogoAlt() ?>” width=”262″ height=”71″ />
getChildHtml(‘topSearch’) ?>
getChildHtml(‘topMenu’) ?>
createBlock(‘Page/Html_Header’);
$header->setTemplate(‘page/html/header.phtml’);
echo $header->toHTML();
does not process these blocks:
getChildHtml(‘topSearch’) ?>
getChildHtml(‘topMenu’) ?>
Thanks
Apr 23, 2010 @ 23:32:47
Hi James,
Try adding $this:
Thanks 🙂
Apr 24, 2010 @ 00:01:10
sorry that is the correct syntax I have – the form submission seems to have removed the php open tag and the ‘this’ reference
Apr 26, 2010 @ 17:25:22
Hello James!
Try using an ‘echo’ also, someone used it here as well.
Let me know the result 🙂
Apr 24, 2010 @ 04:34:04
This looks really promissing but when trying on WordPress 2.9.2 and Magento 1.4.0.1 I get this horror message:
Fatal error: Uncaught exception ‘Mage_Core_Model_Store_Exception’ in /home/d32751/public_html/app/code/core/Mage/Core/Model/App.php:1228 Stack trace: #0 /home/d32751/public_html/app/code/core/Mage/Core/Model/App.php(760): Mage_Core_Model_App->throwStoreException() #1 /home/d32751/public_html/app/Mage.php(322): Mage_Core_Model_App->getStore(NULL) #2 /home/d32751/public_html/app/Mage.php(334): Mage::getStoreConfig(‘web/url/use_sto…’, NULL) #3 /home/d32751/public_html/app/code/core/Mage/Core/Controller/Request/Http.php(196): Mage::getStoreConfigFlag(‘web/url/use_sto…’) #4 /home/d32751/public_html/app/code/core/Mage/Core/Controller/Request/Http.php(148): Mage_Core_Controller_Request_Http->_canBeStoreCodeInUrl() #5 /home/d32751/public_html/app/code/core/Mage/Core/Model/App.php(379): Mage_Core_Controller_Request_Http->setPathInfo() #6 /home/d32751/public_html/app/code/core/Mage/Core/Model/App.php(262): Mage_Core_Model_App->_initRequest() #7 /home/d32751/public_html/app/Mage.php(570): Mage_Core_Model_App->init(‘default’, ‘st in /home/d32751/public_html/app/code/core/Mage/Core/Model/App.php on line 1228
Any ideas?
Apr 26, 2010 @ 17:42:04
Check the permission of the following folders:
Magento must have read/write access to these folders. Also clear the sessions and cache folder contents within /var.
Apr 24, 2010 @ 14:12:44
Is anybody gettting this to work with Magento 1.4.0.1 and WordPress 2.9.2? I get a fatal error. Maybe it needs to be altered for these versions?
Apr 26, 2010 @ 17:42:33
Check my reply above your comment 🙂
Apr 26, 2010 @ 07:32:31
hi Richard
I have solved the immediate problem by moving the nav blocks to outside the header area and referencing the html createBlock seperately
However I still have some inner htmls in the header block which is not being rendered
getChildHtml('seo.searchterm') ?>Also another one in footer
getChildHtml('seo.searchterm') ?>Any thoughts on why any of the html inner blocks are not being redered?
Apr 26, 2010 @ 07:34:51
I have used the code tag to paste the code but the wp seems to truncate my opening php tag, echo statement and the class refrence. Am I doing somethign wrong?
Apr 26, 2010 @ 17:30:45
Check this out. This is how is display source code in my post and comments.
Apr 26, 2010 @ 18:10:08
It seems your script should work now but I can’t see your whole script. Also try checking this post so you can validate also if you accidentally missed something.
Apr 28, 2010 @ 23:05:11
Hi Richard
Thanks for taking the timur e to respond to my queries.
My script is working is all ok and works fine on my magento rendered pages. The issue is only in the word press header.
Re-pasting my code using your instructions:
<div id="header"> <div id="logo"> <a href="<?php echo $this->getUrl('') ?>" title="<?php echo $this->getLogoAlt() ?>" class="logo"> <img src="<?php echo $this->getLogoSrc() ?>" alt="<?php echo $this->getLogoAlt() ?>" width="262" height="71" /></a> </div> <div id="basketPanel"> <?php echo $this->getChildHtml('topLinks') ?> <a href="<?php echo $this->getUrl('checkout/cart') ?>"> <img src="<?php echo $this->getSkinUrl('images/basket.png') ?>" alt="Shopping Basket" width="79" height="23"></a></td> <a href="<?php echo $this->getUrl('checkout') ?>"> <img src="<?php echo $this->getSkinUrl('images/checkout.png') ?>" alt="Basket Checkout" width="79" height="23"></a></td> </div> </div>Apr 28, 2010 @ 23:18:10
Is this your code in your .phtml file for header?
Apr 29, 2010 @ 00:41:32
yes thats right Richard ( its obviously been customised from the base file )
Apr 29, 2010 @ 17:46:35
It looks fine to me. What happens when your try to run your script?
Apr 26, 2010 @ 19:54:54
Thanks for the feedback! I’ve tried to change the permissions as you suggest and also clear seassion and cache data but still the same error. In fact I’ve tried to chmod 777 the whole site but still the same error.
In the past I’ve had some errors because my server run php as a module. Could this cause errors?
Apr 26, 2010 @ 20:09:28
Hmm weird. Can you run Magento as a standalone application without any error?
Apr 26, 2010 @ 20:28:20
Check the links below too, it seems the issue is related to what they experienced too with Magento.
http://www.magentocommerce.com/boards/viewthread/78280/#t219655
http://www.magentocommerce.com/boards/viewthread/52951/
Apr 26, 2010 @ 21:23:03
I get it to work on version 1.4.0.0 strangely enough.
Apr 27, 2010 @ 13:50:21
That’s good then.
Apr 27, 2010 @ 03:42:17
Sorry to bother you with these boring errors. This time I’ll only give you some feedback.
1, I did get the code running! But I didn’t change the code. Instead I tried it from my Macbook which uses another version of Firefox and to my surprise it started working! When I however try from Safari on the Macbook it still gives me the same error.
2. I did a fresh install of Magento 1.4.0.1 through ssh on the same server (I think I used ftp last time). This time I get the code working again (all browsers).
I don’t know what conclusions to draw but I just wanted to leave some feedback.
Apr 27, 2010 @ 13:50:05
Well looks like your problem is actually your browser cache.
Apr 27, 2010 @ 18:56:02
Ha, nailed it!!!
The problem lies in the internal Store View Codes. I run three stores and I’ve changed the Store View Codes to make sense to me. While doing that I removed the Code ‘default’. So when I put ‘default’ back it starts working. Magic!
Many thanks for great feedback and code examples!
Apr 27, 2010 @ 19:25:11
Great! Good thing you found a fix for that one. 🙂
Apr 28, 2010 @ 01:36:35
Richard,
Do you have any ideas on how you could query both Magento and WP’s db at the same time, so that the search function will return posts and catalog items?
Apr 28, 2010 @ 01:49:35
Hi Peaker,
Why would you want to combine two different sets of data into 1 query? Create a separate query for post and catalog then do your manipulation in arrays using PHP.
Apr 28, 2010 @ 02:20:24
Good point. Thanks Richard.
Apr 30, 2010 @ 05:47:21
I have a question.
This work-around may solve my problem.
I have a website (Xhtml/css). I need to add Magento to it as well as a CMS for the static & blog pages (more static pages then blog).
I am considering putting my website into WordPress. And then creating a Magento site/theme that looks just like it. So it would be mywebsite.com and shop.mywebsite.com
On my homepage I need to pull magento products into a slider/carousel. Will this workaround help with that?
I also need to have the header and footer from Magento. (ie “Log-in/Register” and “Your cart has 2 items”).
Is this even the best method.
Apr 30, 2010 @ 10:22:54
Hello Erica!
You want to use WordPress for the homepage with the login/register and cart info in it pulling information from Magento while the subdomain ‘shop’ will showcase an actual Magento store? Yes, it is possible 🙂 Although you will be managing two sites now since Magento has its own templating system. How about using WordPress for everything from blogging to the actual shop too so that all you need to skin is WP (who in turn handles your CMS and shop’s frontend) and catalogs and orders management will be handled at the backend of Magento?
Apr 30, 2010 @ 19:15:42
Richard,
So I will be able to do front-end WP and back-end Magento with this workaround?
Just so I understand. Inside a WP template I can have my Magento products displayed on the page. I can interact with Magento as I normally would inside of WP?? If, so this sounds like the solution to my problem. I think I struggle with the logic more than anything.
Apr 30, 2010 @ 20:04:48
Yeah, you’re right. It’s more like you just have to create your pages for the required functionality from Magento to WordPress pages such as product display/search, viewing product details, cart functions and customer related tasks. I did a similar project last year but instead of using WordPress, I used CodeIgniter framework while Magento would be my backend. Everything can be done thru Mage.php file 🙂
May 01, 2010 @ 01:19:11
Hey Richard,
Thanks for taking the time to post this article – exactly what I am looking for. I have ran through the article a couple times and have a couple questions:
1. To keep from modifying the core of WP can I assume it would be okay to place the magento() function inside functions.php of my theme? (I did similar by making the change to magento’s functions.php in code/local/Mage/Core).
2. In WordPress (2.9) mage is working and I can print_r the session object but I can not seem to access any data from mage (user name, cart items etc) – I simply get nothing. Magento is 1.4.0.1.
Any Suggestions?
Kind Regards,
Bret
May 01, 2010 @ 11:10:49
Hi Bret!
Thanks for reading my blog. Regarding item number 1, it really doesn’t matter whether you place your magento() function inside functions.php or within your theme files as long as you get the same result. I just placed it within functions.php for it to be available anywhere within WordPress, not just my theme files. Just make sure that no header has been sent prior to calling the ‘core/session’ of Magento.
For your item number 2, how do you use the script for accessing customer info and cart contents? You should be able to access it already since the session’s object can be printed as you mentioned earlier. Check as well the error_reporting of your php settings. It must be set to ‘all’ while in development stage so you can see if there’s an error in your script.
Regards and good luck!
May 05, 2010 @ 13:20:44
Hi Richard!
Great idea for wordpress integration. This looks so simple that it is hard to believe!
So it is really possible to pull everything in Magento into WordPress?
Even payment modules? It would be really cool to find a live example.
Pure genius!
May 05, 2010 @ 15:06:37
Thanks for the feedback!
Yes, you should be able to utilize the payment modules present in Magento to your WordPress instance. For example, the script below can be used in order to pull the list of active payment methods from Magento to WordPress:
magento(); // Get collection of active payment methods $activepayments = Mage::getSingleton('payment/config')->getActiveMethods(); // Create the initial dropdown option $methods = array(array('value'=>'', 'label'=>Mage::helper('adminhtml')->__('--Please Select--'))); // Loop through active payment list and push it to $methods array foreach ($activepayments as $paymentCode=>$paymentModel) { $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title'); $methods[$paymentCode] = array( 'label' => $paymentTitle, 'value' => $paymentCode, ); } // Generate dropdown echo "<br><br>"; echo "<select name=\"active_payments\">"; foreach($methods as $key => $value){ echo "<option value=\"".$value['value']."\">".$value['label']."</option>"; } echo "</select>";This should produce an output like the one below:
You may check your other options in the Magento docs 🙂
May 06, 2010 @ 00:07:42
Hi Richard,
I just stumbled upon your post and I must say it looks really great.
I am going to give it a try today.
If it works as expected this is awesome. Do you plan to release it as a WP plugin anytime? That would be a really popular one, no doubt.
Thanks again.
May 06, 2010 @ 02:34:55
Hello Olivier,
Thanks for reading my blog. Actually that is my next target article. I’m just figuring out for a couple of days already how I can make it work seamlessly via ‘hook’ in WP processes without getting ‘cannot send header…’ error. Hopefully I can finish it soon.
Let me know if it works for you 🙂
May 06, 2010 @ 03:59:12
Sounds good 🙂
Let me know if you need any help with the plugin, would be a pleasure.
May 24, 2010 @ 23:34:27
Check out the WordPress plugin version of this tweak. https://mysillypointofview.wordpress.com/2010/05/11/mage-enabler/
May 11, 2010 @ 18:56:51
May 14, 2010 @ 11:18:13
In a word excellent! Definitely more transparent + documented + supported than lazymonks in my opinion
May 14, 2010 @ 12:11:08
Thank you timani =p
May 26, 2010 @ 16:46:09
Currently using WordPress (3.0-beta2-14729)
and
Magento 1.4
WordPress is here:
http://www.pffmaui.com/blog/
and Magento is here:
http://www.pffmaui.com/
I followed your steps and implemented all the code etc.
Also, uploaded the mage enabler to here:
/html/blog/wp-content/plugins/mage-enabler
When I tried to activate plugin – it gave me “The plugin does not have a valid header.” error.
Also, when I try and link to sub-page for blog – the page comes up empty:
http://www.pffmaui.com/blog/plumeria
thanks for all your support.
Aloha,
Greg
May 26, 2010 @ 18:24:27
UPDATE:
Was able to change the path from this:
/**
* Run Magento’s Mage.php within WordPress
*
* @author Richard Feraro
* @link https://mysillypointofview.wordpress.com
*
* @return object
*/
function magento($name = “frontend”) {
// Include Magento application
require_once ( “..magento/app/Mage.php” );
umask(0);
// Initialize Magento
Mage::app(“default”);
return Mage::getSingleton(“core/session”, array(“name” => $name));
}
to this:
/**
* Run Magento’s Mage.php within WordPress
*
* @author Richard Feraro
* @link https://mysillypointofview.wordpress.com
*
* @return object
*/
function magento($name = “frontend”) {
// Include Magento application
require_once ( “../app/Mage.php” );
umask(0);
// Initialize Magento
Mage::app(“default”);
return Mage::getSingleton(“core/session”, array(“name” => $name));
}
The “Welcome Guest” message comes up on the pages where I placed it, but it doesn’t update when person is logged in.
Thanks again,
Greg
May 26, 2010 @ 18:32:46
Oh I see. Is it the functions.php the file where you added the code above? Restore it again to its original and use the Mage Enabler plugin from the Official WordPress Plugin repo then let me know how it goes.
May 26, 2010 @ 18:47:23
Do not edit functions.php anymore if you have the Mage Enabler plugin installed. I also checked your site, the ‘frontend’ cookie is properly generated which means Mage object is instantiated.
May 26, 2010 @ 19:48:29
OK –
Removed the added functions.php file:
app/code/local/Mage/Core/functions.php
Tried to activate the plugin and received following error message:
The plugin does not have a valid header.
May 26, 2010 @ 19:53:54
Check the folder structure. It should be similar to the setup below:

May 26, 2010 @ 20:03:49
Here’s the set-up:
root: /html/blog/wp-content/plugins/mage-enabler/mage-enabler
with the correct images and files.
May 26, 2010 @ 20:12:01
Which is wrong. If you check the Mage Enabler folder setup image, there’s no mage-enabler folder inside plugins/mage-enabler but rather the actual files including the main php file. In your setup, the main php file is found in plugins/mage-enabler/mage-enabler/mage-enabler.php
May 26, 2010 @ 20:25:00
Yes – jumped back a folder and now the install works.
What code do I need to input to get the Welcome messge to show up?
Thanks again,
Greg
May 26, 2010 @ 20:43:34
Follow the instructions in this post
May 31, 2010 @ 15:58:47
I’ve transferred my blog to a new domain. For comments and inquiries regarding this article, please post it here.
Very useful Wordpress plugin to connect with Magento
Feb 25, 2011 @ 12:30:37