BLOG

Sometimes nerdy and wordy, but mostly helpful articles on website design and development.

Magento and WordPress go together like Chocolate and Peanut Butter

| 42 Comments | Magento, WordPress

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.

42 responses to “Magento and WordPress go together like Chocolate and Peanut Butter

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  14. Hi Gregg,

    I have a magento store wanted to know how do I integrate my wordpress blog to appear in my magento store with the same layout as my store(same theme).

    1. There are a couple of ways to do this. One would be to use the fishpig WordPress extension for Magento or you integrate them so that you can use wordpress functions in Magento and pull in the header/footer/styles that way, which is often what we do.

  15. This is great. I didn’t even think of using this on a recent project. Wish I would have.

  16. Thank you so much for this! You are amazing! I’ve been hunting for a solution for a while and it seems this could be perfect.

    I’m trying to install wordpress into my Magento site (use wordpress as Magento CMS). Fishpig is not an option for us since we need to make use of other wordpress plugins that are not supported. So we need wordpress posts/pages but using the Magento theme.

    In your post it says to place all your code into the Magento index.php. I have done this but it throws a syntax error. And in the comments people talk about placing something in functions.php also? Did I miss something, could this be my problem?

    I’m using James Kemp plugin instead of Mage Enabler but they work in a similar fashion: https://wordpress.org/support/plugin/magento-wordpress-integration

    Any advice on how to set this up would be amazing!

    1. Hi Ben,

      Sorry for the slow reply. I’ve started using James Kemp’s plugin as well. If you have WP in the root of the site and Magento in a sub-folder of the root, your one line of code for Magento’s index.php file would be
      require( '../wp-blog-header.php' );

      You can put it right before the $compilerConfig call. If you still have a syntax error, reply with the actual error and I can help from there.

      1. Hi Greg,

        I am using MWI from James Kemp and trying to include WordPress function on my Magento site. My wordpress is on root and Magento on sub-folder

        I keep getting the following error
        Mage registry key “controller” already exists

        here is a piece of code where I’ve included the WP file.

        define(‘WP_USE_THEMES’, false); require_once(‘../wp-load.php’);
        $mageFilename = MAGENTO_ROOT . ‘/app/Mage.php’;
        $maintenanceFile = ‘maintenance.flag’;

        PS: tried to load both wp-load.php & wp-blog-header.php and same error.

  17. great post .. but i have done this before, my problem is that any shortcode from the plug in installed in wordpress, is not working with the magento , i want to call a interactive image through the shortcode, in the admin panel it is working fine but in frontend it is showing the shortcode instead of images .

    Any idea to call the image in the blog using magento will be appreciated.

    Thanks in advance for help..!!

Comments are closed.

Get the Latest

  • Get email updates whenever we post new blog articles.
  • This field is for validation purposes and should be left unchanged.

Recent Articles