David
David

Reputation: 1

Integrating wordpress with magento

I am trying to integrate wordpress into a magento install but keep getting the following error when trying to include

Fatal error: Cannot redeclare __()

I assume this is because both magento and wordpress use this.

How can i get around this.

I have tried things like:

if(!function_exists('__()')) {
    function __() {}
}

in both magento and wordpress files but it makes no difference - granted i dont fully understand what its doing.

Are there any suggestions on getting around this?

Upvotes: 0

Views: 1713

Answers (3)

Matthew Denison
Matthew Denison

Reputation: 21

You can modify your WordPress code. It allows you to integrate both WordPress and Magento easily. I used SSH to get into the server and went into the blog directory. I then ran the following commands:

grep -rl '__(' * | xargs sed -i 's/__(/__wp(/g'

This allowed me to change all references to the '_' function to '_wp' instead with no more redefine errors.

There are some issues to be aware of when using this method:

  1. I would keep the commands handy in a file for running it after you have done any type of installation or upgrade since any type of code modification on your system could potentially bring it down with errors.
  2. I would not recommend using this approach where you do not have full control of the WordPress installation. You really need to have control over this to make sure you don't bring the site down or that someone else doesn't inadvertently cause a problem.

My method of upgrading is:

  1. Redirect all traffic from the blog using (mod_rewrite)
  2. Perform the upgrade(s)
  3. Test
  4. Disable redirects to allow traffic through again

This has worked well for me.

http://www.technickels.com/2012/12/wordpress-integration-with-magento/

Upvotes: 2

Joe Mastey
Joe Mastey

Reputation: 27099

As stated already, the issue occurs because both frameworks define the same function (__()). In order to solve these problems, you need to remove one of the declarations (or make it conditional as listed). IF they happen to be identical functions in both frameworks this is fine, but if either one of them implements the function differently you will need to move one of the declarations to another function (i.e. __2()) and refactor the existing code to point to it. This is a bad idea.

This is actually a good example of why Wordpress and Magento are not easily closely tied together. My first suggestion for getting around this would be to keep the two codebases at arm's length and use mod-rewrite to take care of integrating them if at all possible. Failing that, and depending on requirements, use Wordpress installed elsewhere to manage posts and grab the database information with a wrapper.

When the codebases change (e.g. you upgrade), having integrated the two codebases will likely cause you much existential angst.

Hope that helps!

Thanks, Joe

Upvotes: 1

Paul Grigoruta
Paul Grigoruta

Reputation: 2416

Change that to:

if(!function_exists('__')) {
    function __() {}
}

Upvotes: 2

Related Questions