Magento and WordPress go together like Chocolate and Peanut Butter

Feb 11, 2011 | | 14

I can’t deny my love for chocolate and peanut butter together. They are a perfect match. Similarly, WordPress and Magento have been a go to tandem for ecommerce websites that we design and build.

There are several ways to get the two applications to talk to each other. I’ll go over a way to integrate both Magento into WordPress and WordPress into Magento. This way you can leverage the awesome CMS capabilities of WordPress and the powerful ecommerce Magento.

Magento into WordPress

My favorite route of getting Magento related content on a WordPress page/post is to use the plugin called Mage Enabler by Richard Feraro. It’s pretty easy to setup for an experienced developer. You just give the absolute local path to the Mage.php file in the Mage Enabler settings and then copy the ‘/magento/app/code/core/Mage/Core/functions.php’ to the ‘/magento/app/code/local/Mage/Core/functions.php’ and adjust the translator function that both Magento and WordPress share. You can go to the Plugin page for more info.

The great thing about this plugin is you can bring into your WordPress install any session, cookies or variables that were previously exclusive to Magento. This way you can pull in the mini shipping cart that will carry over the number of items in the cart on the WordPress pages. A great feature to have!

WordPress into Magento

The other way to integrate is to get your WordPress content and variables onto your Magento pages. I’ve seen several different approaches to this, but I’m going to stick with my favorite way, because its the easiest, fastest and doesn’t require a ton of steps like some of the others do.

If you’re already using the Mage Enabler plugin, this is only a 1 step setup!
Navigate to your index.php file in your Magento installation. We are going to add 2 lines of code. 1 will say we’re not using WP_THEMES and the 2nd will require the wp_load.php file that way we can use the WordPress functions.

define('WP_USE_THEMES', false);
require_once('/home/username/public_html/media/wp-load.php');

I have my WordPress installed in the media folder, so i referenced that. The second step is to edit the __() function so that it’s only declared once.

if (!function_exists('__')) {
function __() {
return Mage::app()->getTranslator()->translate(func_get_args());
}
}

Conclusion

And there you have it! 2 great ways to combine the power of both WordPress and Magento. This really just breaks the shell of what we can do with the two married together.

Find this post helpful? Share it!

14 Responses to “Magento and WordPress go together like Chocolate and Peanut Butter”

  1. James 12 May 2011 at 4:53 am #

    Hello, Nice post. I thought I’d let you know about a plugin I have just released. Wordgento (http://www.tristarwebdesign.co.uk/wordpress-plugins/wordgento/).

    It works in a similar way to Mage Enabler, except it does most of the work for you. It offers short snippets of code to bring in most of the existing Magento blocks. Try it out!

    • Gregg 22 May 2011 at 9:08 am #

      very nice! I’ll have to look it over some more. Might use it in my next WordPress Magento site.

  2. Travis 13 June 2011 at 12:04 pm #

    Looks like a great solution! Where in the Magento index.php file are you placing the lines of code? I can’t seem to get mine to work. Getting some sort of Autoload error in Magento. Thanks!

    • Gregg 13 June 2011 at 12:10 pm #

      Hi Travis. The two lines I added to the Magento index.php file were the very first two lines after the comments of license and disclaimer. Make sure the require_once points to the correct location. That might be your hang up. If you still need help, reply back.

      • Travis 13 June 2011 at 1:16 pm #

        Gregg – Thanks for your prompt response. My problem was when I copied the __() function from above to the functions.php file it threw an error. Looks like you’re missing a closing “)”. After turning on error reporting and got that fixed it works like a charm! This is by far the best solution out there. So simple! Thanks again for your contribution!

        • Gregg 13 June 2011 at 1:22 pm #

          ah, good catch! it was the translate function that didn’t have a closing “)”. thanks!!

  3. fearlex 15 June 2011 at 2:22 am #

    This looks awesome and than you for sharing but my problem is a little different. I have wordpress installed at the root level, and magento in a subfolder. i want to use the navigation and the footer from the wordpress site in magento, since user can modify this at will.

    Would this be posible still with your approach?

    Thanks in advance.

  4. fearlex 15 June 2011 at 2:25 am #

    Also they are installed in different databases.

    Another question is, do i definitively need to install Mage Enabler for this to work, or can i skip this step ?

    Thanks

    • Gregg 15 June 2011 at 6:22 am #

      It shouldn’t matter about the databases and I sometimes put wordpress at the root level and Magento in a subfolder. If you want Magento stuff in WordPress you’ll need to install Mage Enabler, but it sounds like you only want WP stuff in Magento, so you mught be able to get by without it.

  5. fearlex 15 June 2011 at 9:02 am #

    Hey Gregg

    Thank you so much for replying. Basically yes, i need WordPress stuff on Magento, mostly the footer and a custom navigation that is managed from wordpress.

    Thank you

  6. fearlex 15 June 2011 at 2:11 pm #

    Hi Greg

    It worked like a charm. I would add the function __() is in a functions.php folder which is located in /magento/app/code/core/Mage/Core/functions.php, ideally it should be copied to /magento/app/code/local/Mage/Core/functions.php since magento will use that instead.

    The only problem i face right now, is that i’m trying to include the function get_header() from wordpress so i could retrieve the whole header, but if i put this function on the header.phtml of my theme, then, it will repeat my doctype twice. So maybe i need to get just the pieces from the header that i need.

    But it still works and is great.

    Thanks for this article and your help, really helpful.

  7. Nick 9 August 2011 at 11:13 pm #

    Adding in that if statement to function.php is causing my quotes to be escaped. Magento acts as if magic_quotes_gpc was enabled, even though it’s not.

    Could it somehow be affecting the function at the top of function.php, which is intended to disable magic_quotes_gpc if it is enabled? If so, how can I counteract this?

    • Nick 10 August 2011 at 5:00 pm #

      I figured out the problem. It turns out that WordPress has it’s own function to add in slashes. As of WordPress version 3.2.1, you can find function wp_magic_quotes() around line 530 of /wp-includes/load.php

      To fix the issue, I commented out everything within the function (not the function itself, so as to prevent a call to an undefined function). It’s removed the issue of escaped quotes. I haven’t done extensive testing, but from what I understand, this may break older WordPress plug-ins, so be careful.

      • Gregg 10 August 2011 at 5:10 pm #

        Sorry for not responding earlier to your question Nick. I find it odd that the if statement was causing it. All it does is check if the function __ is already declared.


Leave a Reply