Magento and WordPress go together like Chocolate and Peanut Butter

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');</pre>
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.
<pre lang="php">if (!function_exists('__')) {
function __() {
return Mage::app()-&gt;getTranslator()-&gt;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.

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

  1. 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!

    Reply

  2. 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!

    Reply

    • 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.

      Reply

      • 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!

        Reply

  3. 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.

    Reply

  4. 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

    Reply

    • 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.

      Reply

  5. 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

    Reply

  6. 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.

    Reply

  7. 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?

    Reply

    • 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.

      Reply

      • 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.

        Reply

  8. Hi,

    thanks for the nice post. I have installed the mage enabler and follow your steps. Now I have a wp blog that I would like to capitalize on, but it does not display with the same theme as my magento site. how do i transfer the magento navigation block, top links, sidebar and footers to wordpress so it would function as if it is a sub from the magento store; i.e. blog or news section of the store?

    many thanks.

    Reply

    • You can pull the blocks like so

      $block_header = $_layout->createBlock('page/html_header')->setTemplate('page/html/header.phtml');

      I haven’t tested that, but it should work.

      Reply

    • Paul: Ahh, I didn’t even think of that. I’ll have to try it out and see how it works. The only thing though is that this will not work for any verison of Magento earlier than 1.4. At this point, I would need the ability to send error reports via email for verisons all the way down to 1.2 or 1.1.

      Reply

  9. [...] How much do you really blog on your Magento shop? Integrating a WordPress blog into Magento Magento and WordPress go togather like Chocolate and Peanut Butter Horizontal Product Slider Magento Extension MasterPass Master Password for Magento Magento [...]

    Reply

  10. nice writing, helped me to integrate my clients blog into Magento theme

    Reply

  11. Hi Gregg.

    You wrote an awesome article, but is there any way I can grab my word press header to replace my magneto’s one ? Is that possible ?

    Reply

    • Thanks for the compliment. I think to accomplish that it’s as easy as including the wordpress wp-blog-header.php file and then call get_header(). You’d have to make sure you fixed the issue of the __() function though.

      Reply

  12. Hello, Just wanted to let you know about my latest plugin – Magento WordPress Integration.

    http://wordpress.org/extend/plugins/magento-wordpress-integration/

    The plugin is simple to install, and will allow you to get any block defined in your Magento XML files! It’s very easy to use, and the website contains all the documentation.

    There is also an extension that allows you to add products to your posts/pages via a shortcode, or products from any magento category as a widget.

    Check it out and let me know what you think.

    Cheers,
    James

    Reply

  13. Hi,
    Thanks for this useful article. I want to integrate the 2 platforms using wordpress as a front end for an ecommerce site.

    I wonder if with the method you describe I can bring elements from Magento into wordpress like customer registration forms, customer account pages, all the checkout steps, wishlists, etc.

    Or is it better to leave all these different features alone in Magento?

    Thanks!

    Reply

    • You can bring almost anything over as you have access to all of Magento’s functions. But, I wouldn’t recommend it. Magento is already doing all of that work, unless there is a specific need for Magento content (like cart contents, or product information), then leave that work to your Magento install.

      Reply

  14. [...] the original here: How to integrate WordPress and Magento together This entry was posted in WordPress Guide and tagged lbx, ocu, xtb. Bookmark the permalink. [...]

    Reply

  15. I added this wrapper around the __ fuction in functions.php:

    if (!function_exists(‘__’)) {

    }

    However, doing this breaks image uploads in the catalog in 1.7. Anyone else experience this? Am I doing something wrong? Without that wrapper, magento complains that the function __ has already been declared in a WP file.

    Reply

  16. Thank you so much Gregg. For this.

    Reply

  17. If no level of integration or interaction is required, would there be any potential issues in installing WordPress in a /blog directory, creating a new database and setting it up on a sub domain?

    Reply

    • There shouldn’t be any issues. If you’re trying to have the same look on both the WP and Mage installs it may be time consuming making an update, but you should be fine doing that setup.

      Is there a reason you want to do that?

      Reply

  18. Hello Greg

    I added the mage enabler pluing, I want to integrate the wordpress blogs with magento header; So to call the same header in in wordpress what is the method
    could you please help me out in this section

    Reply

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>